I have written a regular expression to match the following criteria
any digit (0-9)
hyphen
whitespace
in any order
length between 10 and 25
([0-9\-\w]{10,25})
I am using it to detect payment card numbers, so this works:
Regex.IsMatch("34343434343434", "([0-9\\-\\w]{10,25})"); // true
But this also works:
Regex.IsMatch("LogMethodComplete", "([0-9\\-\\w]{10,25})"); // true
What am I doing wrong?
This is C#
Take a look at Regular Expression Language - Quick Reference, section Character Classes.
\w matches any word character including underscore, not whitespace.
To match whitespace, you can use \s.
To match digits, you can use \d.
Instead of using \w you can use \d which means digit you could use regex like
"[\d\-\s]{10,25}" to match your criteria
You don't need to check for "words" and this is what \w does
Related
i have this expression and i need to make sure to include at least one non-alphabetic character
^(?!.*(.)\1)\S{8,12}$
testhis invalid
testhis7 valid
testhis# valid
You could use a positive lookahead asserting at least 1 char other than a-zA-Z
^(?!.*(.)\1)(?=.*[^\sa-zA-Z])\S{8,12}$
Explanation
^ Start of string
(?!.*(.)\1) Assert not 2 consecutive chars
(?=.*[^\sa-zA-Z]) Assert 1 char other than a whitespace char and a-zA-Z
\S{8,12} Match 8-12 non whitespace chars
$ End of string
Regex demo
Another option is to use \P{L} to assert any char other than any kind of letter from any language
^(?!.*(.)\1)(?=.*\P{L})\S{8,12}$
Regex demo
You can just check for the special character (as matched by [\p{P}\p{S}]) in positive lookahead (?=.*[\p{P}\p{S}]), which gives you the regex:
^(?!.*(.)\1)(?=.*[\p{P}\p{S}])\S{8,12}$
See online demo
You can also replace [\p{P}\p{S}] by [!"\#$%&'()*+,\-./:;<=>?#\[\\\]^_‘{|}~], or any other character set that list all the characters that you want to count as being "special characters".
It's better to do it with separate if-statements. This way you'll have exact information what is missing in the value. With regexps you'll only get a true/false result if the value matched the pattern or not - you'll have no information WHAT is missing in the value.
For example:
if(!value.Any(c => !char.IsLetter(c)){
throw new Exception("value must contain at least one non-letter")
}
I am trying to build a regular expression (using .Net's RegEx object) to match the sequence of characters "C#" with word boundaries.
So searching inside the string "I am a C# developer, but I am not a C#developper", I am trying to match the first "C#" (as a word) but not the second "C#" that is a part of a word.
I have tried the pattern "\bC#\b", with no matches.
I have also tried the pattern "\bC#\b" (trying to escape the #), no matches.
I have read somewhere that the pound (#) sign can be interpreted as word boundary. Is this true? And if so, how can we look for that string ("C#") as a word?
The \b does not match between the pound sign and a space because they both match non word characters but is does match between the pound sign and the d char.
Instead of a second word boundary \b, you could assert that what is on the right is not a non-whitspace \S character using a negative lookahead (?!:
\bC#(?!\S)
Regex demo
As pointed out in the comments by #elgonzo, to prevent breaking the match when a non word char follows C#, you could use a positive lookahead to assert what is on the right is either a non word char \W or assert the end of the string $
\bC#(?=\W|$)
Regex demo
The following RegEx matches first word/last word/new line/spaces case insensitive
/(:?^|\s)C#(:?$|\s)/i
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)
What is the regular exp for a text that can't contain any special characters except space?
Because Prajeesh only wants to match spaces, \s will not suffice as it matches all whitespace characters including line breaks and tabs.
A character set that should universally work across all RegEx parsers is:
[a-zA-Z0-9 ]
Further control depends on your needs. Word boundaries, multi-line support, etc... I would recommend visiting Regex Library which also has some links to various tutorials on how Regular Expression Parsing works.
[\w\s]*
\w will match [A-Za-z0-9_] and the \s will match whitespaces.
[\w ]* should match what you want.
Assuming "special characters" means anything that's not a letter or digit, and "space" means the space character (ASCII 32):
^[A-Za-z0-9 ]+$
You need #"^[A-Za-z0-9 ]+$". The \s character class matches things other than space (such as tab) and you since you want to match sure that no part of the string has other characters you should anchor it with ^ and $.
If you just want alphabets and spaces then you can use: #"[A-Za-z\s]+" to match at least one character or space. You could also use #"[A-Za-z ]+" instead without explicitly denoting the space.
Otherwise please clarify.
In C#, I'd believe it's ^(\w|\s)*$