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
Related
I want to validate full name in contact form. I want to restrict spaces in alphabets. textbox should only accept a-z characters.
I used this regular expression
ValidationExpression="[a-zA-Z ]*$"
But it allows spaces also.
Your regex doesn't work because it contains spaces in the character squance.
You can specify the pattern correctly as
ValidationExpression="^[a-z]*$"
^ Anchors the regex at the start of the string.
[a-z]* Matches zero or more characters
$ Anchors the regex at the end of the string.
Regex Demo
EDIT
To restrict the characters to 50 we could use a quantifier as
ValidationExpression="^[a-z]{,50}$"
{,50} Quantifier ensures that there can be a maximum of 50 characters.
Just remove the space inside your character class?
Also anchor the regex so that it matches at the start of a line :
^[a-zA-Z]*$
And take into consideration that ^ and $ can be influenced by the modifier that says it should match at a newline or not
I would just use "^[a-zA-Z]+$".
I think the issue you have is there is a space between the Z and ]. When I tested this it allowed spaces into the regular expression. I also changed the * to + to not allow a blank string.
I'm trying to build a regular expression in c# to check whether a string follow a specific format.
The format i want is: [digit][white space][dot][letters]
For example:
123 .abc follow the format
12345 .def follow the format
123 abc does not follow the format
I write this expression but it not works completelly well
Regex.IsMatch(exampleString, #"^\d+ .")
^ matches the start of the string, and you got it right.
\d+ matches one or more digits, and you got that one right as well.
A space in a regex matches a literal space, so that works too!
However, a . is a wildcard and will match any one character. You will need to escape it with a backslash like this if you want to match a literal period: \..
To match letters now, you can use [a-z]+ right after the period.
#"^\d+ \.[a-z]+"
The dot is a special character in regex, which matches any character (except, typically, newlines). To match a literal ., you need to escape it:
Regex.IsMatch(exampleString, #"^\d+ \.")
If you want to include the condition for the succeeding letters, use:
Regex.IsMatch(exampleString, #"^\d+ \.[A-Za-z]+$")
For you to get yours to match, keep in mind that the period in regular expressions is a special character that will match any character, so you'll need to escape that.
In addition, \s is a match for any white-space character (tabs, line breaks).
^\d+\s+ \..+
(untested)
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,%,.,?,*,&,^,$,(,!,),#,\-,_]"
I am trying to make a regular expression for a string that has at least 1 non alphanumeric symbol in it
The code I am trying to use is
Regex symbolPattern = new Regex("?[!##$%^&*()_-+=[{]};:<>|./?.]");
I'm trying to match only one of !##$%^&*()_-+=[{]};:<>|./?. but it doesn't seem to be working.
If you want to match non-alphanumeric symbols then just use \W|_.
Regex pattern = new Regex(#"\W|_");
This will match anything except 0-9 and a-z. Information on the \W character class and others available here (c# Regex Cheet Sheet).
https://www.mikesdotnetting.com/article/46/c-regular-expressions-cheat-sheet
You could also avoid regular expressions if you want:
return s.Any(c => !char.IsLetterOrDigit(c))
Can you check for the opposite condition?
Match match = Regex.Match(#"^([a-zA-Z0-9]+)$");
if (!match.Success) {
// it's alphanumeric
} else {
// it has one of those characters in it.
}
I didn't get your entire question, but this regex will match those strings that contains at least one non alphanumeric character. That includes whitespace (couldn't see that in your list though)
[^\w]+
Your regex just needs little tweaking. The hyphen is used to form ranges like A-Z, so if you want to match a literal hyphen, you either have to escape it with a backslash or move it to the end of the list. You also need to escape the square brackets because they're the delimiters for character class. Then get rid of that question mark at the beginning and you're in business.
Regex symbolPattern = new Regex(#"[!##$%^&*()_+=\[{\]};:<>|./?,-]");
If you only want to match ASCII punctuation characters, this is probably the simplest way. \W matches whitespace and control characters in addition to punctuation, and it matches them from the entire Unicode range, not just ASCII.
You seem to be missing a few characters, though: the backslash, apostrophe and quotation mark. Adding those gives you:
#"[!##$%^&*()_+=\[{\]};:<>|./?,\\'""-]"
Finally, it's a good idea to always use C#'s verbatim string literals (#"...") for regexes; it saves you a lot of hassle with backslashes. Quotation marks are escaped by doubling them.