What would this C# regex look like?
At least one (1) character in length
Up to seven (7) characters in length
Numeric characters
I have this, but I needs to check for 1-7 digits:
var chequeNumRX = new Regex("^[0-9]+$");
In regular expressions, you can use the repetition operator {min,max}.
var chequeNumRX = new Regex(#"^\d{1,7}$");
The above regex would match \d a minimum of 1 time and a maximum of 7 times.
Note that \d is a shorthand character class equivalent to [0-9].
Just put the range in after your list of charaters:
{1,7} : allows 1 - 7 charaters
e.g
^[0-9]{1,7}$
Related
I'm trying to write a regular expression using C#/.Net that matches 1-4 alphanumerics followed by spaces, followed by 10 digits. The catch is the number of spaces plus the number of alphanumerics must equal 4, and the spaces must follow the alphanumerics, not be interspersed.
I'm at a total loss as to how to do this. I can do ^[A-Za-z\d\s]{1,4}[\d]{10}$, but that lets the spaces fall anywhere in the first four characters. Or I could do ^[A-Za-z\d]{1,4}[\s]{0,3}[\d]{10}$ to keep the spaces together, but that would allow more than a total of four characters before the 10 digit number.
Valid:
A12B1234567890
AB1 1234567890
AB 1234567890
Invalid:
AB1 1234567890 (more than 4 characters before the numbers)
A1B1234567890 (less than 4 characters before the numbers)
A1 B1234567890 (space amidst the first 4 characters instead of at the end)
You can force the check with a look-behind (?<=^[\p{L}\d\s]{4}) that will ensure there are four allowed characters before the 10-digits number:
^[\p{L}\d]{1,4}\s{0,3}(?<=^[\p{L}\d\s]{4})\d{10}$
^^^^^^^^^^^^^^^^^^^^
See demo
If you do not plan to support all Unicode letters, just replace \p{L} with [a-z] and use RegexOptions.IgnoreCase.
Here's the regex you need:
^(?=[A-Za-z0-9 ]{4}\d{10}$)[A-Za-z0-9]{1,4} *\d{10}$
It uses a lookahead (?= ) to test if it's followed by 4 chars, either alnum or space, and then it goes back to where it was (the beggining of string, not consuming any chars).
Once that condition is met, the rest is a expression quite similar to what you were trying ([A-Za-z0-9]{1,4} *\d{10}).
Online tester
I know this is dumb, but must work exactly as required.
^[A-Za-z\d]([A-Za-z\d]{3}|[A-Za-z\d]{2}\s|[A-Za-z\d]\s{2}|\s{3})[\d]{10}$
Not sure what you are looking for, but perhaps:
^(?=.{14}$)[A-Za-z0-9]{1,4} *\d{10}
demo
Try this:
Doesn't allow char/space/char combination and starts with a char:
/\b(?!\w\s{1,2}\w+)\w(\w|\s){3}\d{10}/gm
https://regex101.com/r/fF2tR8/2
We have a security issue where a specific field in a database has some sensitive information in it. I need a way to detect numbers that are between 2 and 8 in length, replace the digits with a "filler" of the same length.
For instance:
Jim8888Dandy
Mike9999999999Thompson * Note: this is 10 in length and we don't want to replace the digits
123Area Code
Tim Johnson5555555
In these instances anytime we find a number that is between 2 and 8 (inclusive) then I want to replace/fill/substitute that value with the number 0 and keep the length of the original digits
End Result
Jim0000Dandy
Mike9999999999Thompson
000Area Code
Tim Johnson0000000
Is there an easy way to accomplish this using RegEx?
You need to provide a static evaluator method that would do the replacing. It replaces digits in the match with zeroes:
public static string Evaluate(Match m)
{
return Regex.Replace(m.Value, "[0-9]", "0");
}
And then use it with this code:
string input = "9999999099999Thompson534543";
MatchEvaluator evaluator = new MatchEvaluator(Program.Evaluate);
string replaced = Regex.Replace(input, "(?:^|[^0-9])[0-9]{2,8}(?:$|[^0-9])", evaluator);
The regex is:
(?:^|[^0-9]) - should be at the start or preceeded by non-digit
[0-9]{2,8} - the to capture between 2 and 8 digits
(?:$|[^0-9]) - should be at the end or followed by non-digit
Just for the clever regex department. This is not an efficient regex.
(?<=(?>(?'front'\d){0,7}))\d(?=(?'back'(?'-front'\d)){0,7}(?!\d))((?'-front')|(?'-back'))
Replace to 0.
/(?<=(?>(?'front'\d){0,7})) # Measure how many digits we're behind.
\d # This digit is matched
(?=
(?'back' # Measure how many digits we're in front of.
(?'-front'\d)){0,7}
# For every digit here, subtract one group from 'front',
# As to assert we'll never go over the < 8 digit requirement.
(?!\d) # no more digits
)
(
(?'-front') # At least one capturing group left for 'front' or 'back'
|(?'-back') # for > 2 digits requirement.
)/x
I need a regular expression for my password format. It must ensure that password only contains letters a-z, digits 0-9 and special characters: .##$%&.
I am using .NET C# programming language.
This is my code:
Regex userAndPassPattern = new Regex("^[a-z0-9.##$%&]$");
if (!userAndPassPattern.IsMatch(username) || !userAndPassPattern.IsMatch(password))
return false;
The problem is that I always get back false.
!A || !B is logically equivalent to !(A && B)
So you could write better
!(userAndPassPattern.IsMatch(username) && userAndPassPattern.IsMatch(password))
Then you have a special character $ in you character class, maybe you need to mask it \$
I'm not quite sure about this, because in a character class it is not a special character. Maybe it depends on the RegEx engine in use. If you mask the $ it should do no harm ([a-z0-9.##\$%&])
Then you have just a single character to match. You need a quantifier
[a-z0-9.##$%&] means one single character out of the given, will match aor b or 0 but not ab
[a-z0-9.##$%&]+ many characters out of the given, from 1 to endless appearances, will match a, b, and ab and ba etc.
edit
This is what you want
Regex userAndPassPattern = new Regex("^[a-z0-9\.##\$%&]+$");
if (!(userAndPassPattern.IsMatch(username) && userAndPassPattern.IsMatch(password))) {
return false;
}
You forgot to add the '+' for matching one or more times:
Regex userAndPassPattern = new Regex("^[a-z0-9.##$%&]+$");
This code Regex userAndPassPattern = new Regex("^[a-z0-9.##$%&]$"); will only match a username or password that is a single character long.
You are looking for something like this Regex userAndPassPattern = new Regex("^[a-z0-9.##$%&]+$"); which will match one or more of the characters in your class. The + symbol tells it to match one or more of the previous atom (which in this case is the character class you specified in the square brackets)
Also, if you did not mean to constrain the match to lowercase characters, you should add 'A-Z' to the character class Regex userAndPassPattern = new Regex("^[A-Za-z0-9.##$%&]$");
You might also want to implement a minimum length restriction which can be accomplished by replacing the + with the {n,} construct, where n is the minimum length you want to match. For example:
this would match a minimum of 6 characters
Regex userAndPassPattern = new Regex("^[a-z0-9.##$%&]{6,}$");
this would match a minimum of 6 and a maximum of 12
Regex userAndPassPattern = new Regex("^[a-z0-9.##$%&]{6,12}$");
You have two problems. First . and $ need to be escaped. Second you are matching only 1 character. Add a + before the last $:
^[a-z0-9\.##\$%&]+$
Edit: Another suggestion, if you have a minimum/maximum length you can replace the + with, for example, {6,16} or whatever you think is appropriate. This will match strings that are 6 to 16 character inclusive and reject any shorter or longer strings. If you don't care about an upper limit, you could use {6,}.
Have you tried using a verbatim string literal when you're using regex escape sequences?
Regex userAndPassPattern = new Regex(#"^[a-z0-9##\$%&]+$");
if (!userAndPassPattern.IsMatch(username) || !userAndPassPattern.IsMatch(password))
return false;
Your pattern only allows a single character set you probably want a repetition operator like * + or {10,}.
Your character set includes . which matches any character, defeating the object of the character class. If you wanted to match "." then you need to escape it with \.
I have the following code to validate usernames for an application:
Regex usernameRegex = new Regex("[A-Za-z0-9_]");
if (usernameRegex.IsMatch(MyTextBox.Text)) {
// Create account, etc.
}
How would I modify my regular expression to check if the username has a certain number of characters?
This expression validates only all text which contains any combination of A to Z, a to z and number 0 to 9. You can define the length of the string using the regex:
Regex reg= new Regex(#"^[A-Z]{3,}[a-z]{2,}\d*$")
{3,} and {2,} mean here that the string must have at least 3 capital characters, at least 2 small characters, and any amount of digit characters.
For example :
Valid : AAAbb, AAAbb2, AAAAAAbbbbb, AAAAAbbbbbb4343434
Invalid: AAb, Abb, AbAbabA, 1AAAbb,
To set a minimum (or maximum) range in a regular expression you can use the {from,to} syntax.
The following will only match a string with a minimum of 5 alpha numeric and underscore characters:
[A-Za-z0-9_]{5,}
And the following will match a minimum of 5 and maximum of 10:
[A-Za-z0-9_]{5,10}
[A-Za-z0-9_]
[] "brackets": are a group of characters you want to match.
A-Z: means it will match any alphabet capitalized within this range A-Z.
a-z: means it will match any small alphabet within this range a-z.
0-9: means it will match any digit in this range 0-9.
_: means it will match the "_" character.
now this regex will usually match the following: any character from a to z (small, capital), any number (from 0-9) and the underscore "_".
i.e. "a.,.B.,10.._" this will match "a, B, 10, _". but of course you need to add the singleline regex option.
I need a regular expression for c# which can match following pattern
abc1abcd
1abcdefg
abcdefg1
basically my expression should have at least one number and min size is 8 char including number. If possible explain the regex also.
I'd probably check with two statements. Just check the length eg
string.Length > 7
and then make sure it this regex can find a match...
[0-9]
You can use a look-ahead assertion to verify the length, and then search forward for a digit, thus:
(?=.{8}).*[0-9]
We look-ahead for 8 characters, and if that is successful, then we actually attempt to match "anything, followed by a digit".
But really, don't do this. Just check the length explicitly. It's much clearer.
Your regular expression pattern should just be: \d+ (match 1 or more numbers). For your example, it's probably best to not determine minimum length using regex since all you care about is that it has at least 1 number and is at least than 8 characters
Regex regEx = new Regex(#"\d+");
isValid = regEx.Match(myString).Success && myString.Length >= 8;
The pattern \d is just the same as [0-9] and the + symbol means at least one of. The # symbol in front of the string is so that it what try to escape \d.
As mentioned by El Ronnoco in the comments, just \d would match your requirement. Knowing about \d+ is useful for more complicated patterns where you want a few numbers in between some strings,etc.
Also: I've just read something that I didn't know. \d matches any character in the Unicode number, decimal digit category which is a lot more than just [0-9]. Something to be aware of if you just want any number. Otherwise El Ronnoco's answer of [0-9] for your pattern is sufficient.