I want to match a string which I divide into 4 groups:
1.) group has a "-"
2.) group has any char
3.) group has a ":"
4.) group has any char
I have tried this:
Regex regex = new Regex("^[-][.*][:][.*]*$");
bool isMatch = regex.IsMatch("-jobid:3");
isMatch is false.
What is wrong in my pattern?
The error here is that .* should not be enclosed in brackets.
This:
[.*]
Means this:
The dot
or the asterix
This:
.*
Means this:
Any character, zero or more times
Additionally, if there is only 1 legal character in a spot, you generally don't need to enclose it in brackets.
So try this expression instead:
new Regex("^-.*:.*$");
You have misunderstood the character class notion. A character class is only a collection of characters without any order. So when you write something like [.*], that means a literal dot or a literal asterisk.
An important precision, all the regex special characters loose their meaning and are seen as literal characters inside a character class. However some characters may have a special meaning in a character class like ^ at the first place (to negate a class) or - to define a range. Some syntaxes can be used too inside character classes, like shorthand or POSIX character classes, and character classes substractions.
You can write the whole pattern without all these (useless) character classes:
^-.*:.*$
However, to be more efficient, you can use a negated character class before the semi-colon:
^-[^:]*:.*$
Related
I m trying to matching a string which will not allow same special character at same time
my regular expression is:
[RegularExpression(#"^+[a-zA-Z0-9]+[a-zA-Z0-9.&' '-]+[a-zA-Z0-9]$")]
this solve my all requirement except the below two issues
this is my string : bracks
acceptable :
bra-cks, b-r-a-c-ks, b.r.a.c.ks, bra cks (by the way above regular expression solved this)
not acceptable:
issue 1: b.. or bra..cks, b..racks, bra...cks (two or more any special character together),
issue 2: bra cks (two ore more white space together)
You can use a negative lookahead to invalidate strings containing two consecutive special characters:
^(?!.*[.&' -]{2})[a-zA-Z0-9.&' -]+$
Demo: https://regex101.com/r/7j14bu/1
The goal
From what i can tell by your description and pattern, you are trying to match text, which start and end with alphanumeric (due to ^+[a-zA-Z0-9] and [a-zA-Z0-9]$ inyour original pattern), and inside, you just don't want to have any two consecuive (adjacent) special characters, which, again, guessing from the regex, are . & ' -
What was wrong
^+ - i think here you wanted to assure that match starts at the beginning of the line/string, so you don't need + here
[a-zA-Z0-9.&' '-] - in this character class you doubled ' which is totally unnecessary
Solution
Please try pattern
^[a-zA-Z0-9](?:(?![.& '-]{2,})[a-zA-Z0-9.& '-])*[a-zA-Z0-9]$
Pattern explanation
^ - anchor, match the beginning of the string
[a-zA-Z0-9] - character class, match one of the characters inside []
(?:...) - non capturing group
(?!...) - negative lookahead
[.& '-]{2,} - match 2 or more of characters inside character class
[a-zA-Z0-9.& '-] - character class, match one of the characters inside []
* - match zero or more text matching preceeding pattern
$ - anchor, match the end of the string
Regex demo
Some remarks on your current regex:
It looks like you placed the + quantifiers before the pattern you wanted to quantify, instead of after. For instance, ^+ doesn't make much sense, since ^ is just the start of the input, and most regex engines would not even allow that.
The pattern [a-zA-Z0-9.&' '-]+ doesn't distinguish between alphanumerical and other characters, while you want the rules for them to be different. Especially for the other characters you don't want them to repeat, so that + is not desired for those.
In a character class it doesn't make sense to repeat the same character, like you have a repeat of a quote ('). Maybe you wanted to somehow delimit the space, but realise that those quotes are interpreted literally. So probably you should just remove them. Or if you intended to allow for a quote, only list it once.
Here is a correction (add the quote if you still need it):
^[a-zA-Z0-9]+(?:[.& -][a-zA-Z0-9]+)*$
Follow-up
Based on a comment, I suspect you would allow a non-alphanumerical character to be surrounded by single spaces, even if that gives a sequence of more than one non-alphanumerical character. In that case use this:
^[a-zA-Z0-9]+(?:(?:[ ]|[ ]?[.&-][ ]?)[a-zA-Z0-9]+)*$
So here the space gets a different role: it can optionally occur before and after a delimiter (one of ".&-"), or it can occur on its own. The brackets around the spaces are not needed, but I used them to stress that the space is intended and not a typo.
I have a problem with that regular expression [^%()*+-\/=?#[\\]ªº´¿'.]*` .
I want to avoid the characters inside. the regular expression it is working but when I set something like DAVID, SC I can save the form because it has a comma but this character it is not inside the regular expression.
Could you help me please?
You are not accounting for the special meaning of - inside a character class [.....].
You must either place the dash at the very end, or else escape it with a backslash:
[^%()*+\/=?#\[\]ªº´¿'.-]*
In your original regex, +-\/ disallows any characters between + and / in the ASCII table; these are the comma, dot and dash. Your example input contains a comma so the regex did not match all of the input at once.
I have also fixed the escaping for the [] characters from [\\] to \[\], which I presume was a mistake.
Because you're using * in [^%()*+\/=?#[\\]ªº´¿'.-]* with line start/end anchors. * means match 0 or more of preceding group/pattern in character class and your regex can even match an empty string.
Use this regex:
^[^%()*+\/=?#[\\-]ªº´¿'.]+$
PS: Hyphen - should be either or first OR at last position in character class to avoid escaping.
Rubular Demo
I try to use regular expression to check if a string contains only: 0-9, A-Z, a-z, \, / or -.
I used Regex validator = new Regex(#"[0-9a-zA-Z\-/]*"); and no matter what string I introduce is valid.
The check look like this: if(!validator.IsMatch(myString))
What's wrong?
If I understand what you want. I believe your pattern should be
new Regex(#"^[0-9a-zA-Z\\\-/]*$");
The ^ and $ symbols are anchors that match the beginning and end of the string, respectively. Without those, the pattern would match any strings that contain any character in that class. With them, it matches strings that only contain characters in that class.
You also specified you wanted to include backslash characters, but the original pattern had \- in the character class. This is simply an escape sequence for the hyphen within the character class. To also include backslash in the character class you need to specify that separately (escaped as well). So the resulting character class has \\ (backslash) followed by \- (hyphen).
Now, this will still match empty strings because * means "zero-or-more". if you want to only match non-empty strings use:
new Regex(#"^[0-9a-zA-Z\\\-/]+$");
The + means "one-or-more".
Use + instead of *
new Regex(#"[0-9a-zA-Z\-/]+");
If I write a Regex of the form
"[some character class]*"
it will match every string. Every string contains 0 to many of a character class.
Perhaps you wanted to use
new Regex(#"[0-9a-zA-Z\-/]+")
to specify 1 to many of your character class.
Pattern is
Regex splRegExp = new System.Text.RegularExpressions.Regex(#"[\,#,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,-,_]");
All characters work except '-'. Please advise.
Use
#"[,#+\\?\d%.*&^$(!)#_-]"
No need for all those commas.
If you place a - inside a character class, it means a literal dash only if it's at the start or end of the class. Otherwise it denotes a range like A-Z. As Damien put it, the range ,-, is indeed rather small (and doesn't contain the -, of course).
'-' has to be the first charater in your regex.
Regex splRegExp = new System.Text.RegularExpressions.Regex(#"[-,\,#,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,_]");
You need to escape the -character for it to work (it's a regular expression syntax)
Try this:
"[\,#,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,\-,_]"
Please provide a solution to write a regular expression as following in C#.NET:
I would require a RegEx for Non-Alphabets(a to z;A to Z) and Non-Numerals(0 to 9).
Mean to say as reverse way for getting regular expression other than alphabets and otherthan numerals(0 to 9).
Kindly suggest the solution for the same.
You can use a negated character class here:
[^a-zA-Z0-9]
Above regex will match a single character which can't be a latin lowercase or uppercase letter or a number.
The ^ at the start of the character class (the part between [ and ]) negates the complete class so that it matches anything not in the class, instead of normal character class behavior.
To make it useful, you probably want one of those:
Zero or more such characters
[^a-zA-Z0-9]*
The asterisk (*) here signifies that the preceding part can be repeated zero or more times.
One or more such characters
[^a-zA-Z0-9]+
The plus (+) here signifies that the preceding part can be repeated one or more times.
A complete (possibly empty) string, consisting only of such characters
^[^a-zA-Z0-9]*$
Here the characters ^ and $ have a meaning as anchors, matching the start and end of the string, respectively. This ensures that the entire string consists of characters not in that character class and no other characters come before or after them.
A complete (non-empty) string, consisting only of such characters
^[^a-zA-Z0-9]+$
Elaborating a bit, this won't (and can't) make sure that you won't use any other characters, possibly from other scripts. The string аеΒ would be completely valid with the above regular expression, because it uses letters from Greek and Cyrillic. Furthermore there are other pitfalls. The string á will pass above regular expression, while the string ́a will not (because it constructs the letter á from the letter a and a combining diacritical mark).
So negated character classes have to be taken with care at times.
I can also use numerals from other scripts, if I wanted to: ١٢٣ :-)
You can use the character class
[^\p{L&}\p{Nd}]
if you need to take care of the above things.
just negate the class:
[^A-Za-z0-9]
To obey local setting use:
[^[:alnum:]]