Unable to understand ASP.NET regular expression example - asp.net

I am going through the documnetation of ASP.NET on Regular Expressions where I stuck at following expression
^[0-9]{5}$
The Input (Mathches) are, 11111, 12345, 55555
Now, from my understanding , first and third are correct (First character can be 0-9 and there must be five occurrences of that character). Please make me understand how second is a valid match.

The {5} means that the match must be repeated, and [0-9] matches any digit. So this matches any 5 digits, not especially 5 identical digits.

Related

Regular expression for x number of digits and only one hyphen?

I made the following regex:
(\d{5}|\d-\d{4}|\d{2}-\d{3}|\d{3}-\d{2}|\d{4}-\d)
And it seems to work. That is, it will match a 5 digit number or a 5 digit number with only 1 hyphen in it, but the hyphen can not be the lead or the end.
I would like a similar regex, but for a 25 digit number. If I use the same tactic as above, the regex will be very long.
Can anyone suggest a simpler regex?
Additional Notes:
I'm putting this regex into an XML file which is to be consumed by an ASP.NET application. I don't have access to the .net backend code. But I suspect they would do something liek this:
Match match = Regex.Match("Something goes here", "my regex", RegexOptions.None);
You need to use a lookahead:
^(?:\d{25}|(?=\d+-\d+$)[\d\-]{26})$
Explanation:
Either it's \d{25} from start to end, 25 digits.
Or: it is 26 characters of [\d\-] (digits or hyphen) AND it matched \d+-\d+ - meaning it has exactly one hyphen in the middle.
Working example with test cases
You could use this regex:
^[0-9](?:(?=[0-9]*-[0-9]*$)[0-9-]{24}|[0-9]{23})[0-9]$
The lookahead makes sure there's only 1 dash and the character class makes sure there are 23 numbers between the first and the last. Might be made shorter though I think.
EDIT: The a 'bit' shorter xP
^(?:[0-9]{25}|(?=[^-]+-[^-]+$)[0-9-]{26})$
A bit similar to Kobi's though, I admit.
If you aren't fussy about the length at all (i.e. you only want a string of digits with an optional hyphen) you could use:
([\d]+-[\d]+){1}|\d
(You may want to add line/word boundaries to this, depending on your circumstances)
If you need to have a specific length of match, this pattern doesn't really work. Kobi's answer is probably a better fit for you.
I think the fastest way is to do a simple match then add up the length of the capture buffers, why attempt math in a regex, makes no sence.
^(\d+)-?(\d+)$
This will match 25 digits and exactly one hyphen in the middle:
^(?=(-*\d){25})\d.{24}\d$

Combining 2 regular expression in web.config passwordStrengthRegularExpression=""

I am not able to combine below two regular expressions. Password standard requirement:
Password cannot contain your username or parts of your full name
exceeding two consecutive characters
Passwords must be at least 6 characters in length
Passwords must contain characters from three of the following categories
Uppercase characters (English A-Z)
Lowercase characters (English a-z)
Base 10 digits (0-9)
Non-alphabetic characters (e.g., !, #, #, $, %, etc.)
Expression:
passwordStrengthRegularExpression="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%]).{6,20})"
Passwords cannot contain the word “Test” or “test” or variants of the word
passwordStrengthRegularExpression="((?=.*\"^((?!Test|test|TEST).*)$"
Both are working fine individually.
Because your second regexp primarily uses a negative lookahead, you can remodel that slightly and stick it right at the beginning of the other expression. First, I'm going to change your second regex to:
"(?!.*(?:Test|test|TEST))"
In english, the string may not contain any number of (or zero) characters followed by test.
Then, I'm going to stick that right at the beginning of your other expression
passwordStrengthRegularExpression="^(?!.*(?:Test|test|TEST))(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%]).{6,20}$"
Finally, I'm going to show you how to make only one part of a regex case-insensitive. This may or may not be supported depending on what program this is actually for.
passwordStrengthRegularExpression="^(?!.*(?i:test))(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%]).{6,20}$"
See the (?i:...)? That means that the flags between the ? and the : are applied only to that part of the expression, that is, only that area is case-insensitive.
Combining your requirements and https://stackoverflow.com/a/2860380/156388 i've come up with this:
(?=^[^\s]{6,}$)(?!.*(?i:test))((?=.*?\d)(?=.*?[A-Z])(?=.*?[a-z])|(?=.*?\d)(?=.*?[^\w\d\s])(?=.*?[a-z])|(?=.*?[^\w\d\s])(?=.*?[A-Z])(?=.*?[a-z])|(?=.*?\d)(?=.*?[A-Z])(?=.*?[^\w\d\s]))^.*
Dont think your first regex is actually working fine if you want to meet the requirements in bullets above it. Clamps to 20 chars but doesn't say you have to. Requires all four of the categories but requirements says 3 of the 4. Doesn't check the username requirement at all. So I've gutted out most of the initial regex.
It matches these (as expected):
Short5
TeSamplePrd6
TEBREaKST6
WinningUser6#
It fails on these (as expected):
SamplePassword
TestUser6#
Shrt5
TeSTTest
Remaining problems
For some reason it matches this:
TEBREKST6
but it only meets two of the four requirements + min length - not sure why?
There is nothing taken into account about the "Password cannot contain your username or parts of your full name exceeding two consecutive characters" requirement and I'm not sure you can even do this through web.config min password requirement as you dont have access to it within the regex.

Alphanumeric RegEx validation

What is the regular Expression Validation for only Letters and Numbers in asp.net?
I need to enter first two should be character after that it can take hyphen(-), space(), apostrophes(')
I tried
^[A-Z a-z\s-'\s]{2,25}$
this is not working.
If I understood what you want, this should work:
^[a-zA-Z]{2}[-\040']*$
This will match two letters followed by any number of hyphens, spaces, or apostrophes. It will match the following strings
ab --
xy'
zz
But will not match these
12
'ab
x-
NOTE: This will not limit the length of the match expression (as your original one did). If that's important replace the * with {,23}.

RegEx to check the string contains least one alphabet or digit

I want a regular expression that check string must contain least an alphabet [a-zA-Z] or a digit. All other special characters are allowed, but only special characters or only spaces or only spaces with special characters will now be accepted.
I have tried /\b(?=[A-Z]*[0-9])(?=[0-9]*[A-Z])[\s\S]\b/i and ^(a-zA-Z0-9).*[\s\S]*$ and ^(a-zA-Z0-9).*[\s].*[\S]*$ etc. But its not working. Awaiting for your valuable response.
Thanks
^(?=.*[\w\d]).+
This pattern will fail if there is not at least one character or one digit with any combination of special characters and spaces.
I'm not sure I understood you correctly, but from what I've gathered you want to have atleast one letter (a-z, 0-9) in the string. This regex will do just that: /^(?=.*[a-z\d]).+/igm
(Set the flags however they need to be set in asp.net. The m-flag might be redundant for you, I only used it for the demo. The g-flag likely does not exist. If so, just remove it.)
Demo+explanation: http://regex101.com/r/jY9fJ5
If you want at least one alphabet or digit, followed by only spaces and symbols:
/^.*[a-zA-Z0-9][^a-zA-Z0-9]*$/
If you want only one alphabet or digit, followed by the same:
/^[^a-zA-Z0-9]*[a-zA-Z0-9][^a-zA-Z0-9]*$/
I can't imagine what else it is that you are looking for. Examples would help immensely.
(?=.*?[0-9])(?=.*?[A-Za-z]).+
Allows special characters and makes sure at least one number and one letter.
(?=.*?[0-9])(?=.*?[A-Za-z])(?=.*[^0-9A-Za-z]).+
Demands at least one letter, one digit and one special-character.
The first one does not demand special chars, only allows them.

regular expression for 5 digit number excluding '00000'

i need to have a regular expression which accept atleast 1 digit number and maximum 5 digit number and if user enter zero in the following fashion like '00','000','0000','00000' then expression should reject such inputs.
currently, i am using ^[0-9]{1,5}$.
If you'd make sure that the user's input is formatted as a 5 digit number with leading zeroes, then the following regex would work:
^[0-9]{5}(?<!00000)$
This uses negative lookbehind to ensure that the string entered was not 5 zeroes.
^(?=.*[1-9].*)[0-9]{1,5}$
Uses a lookahead assertion to make sure there's at least one nonzero digit. If there is one, then the rest of the expression only matches if there's between 1 and 5 digits. Both conditions have to be met, or the expression won't match.
Agreed, though, that if you're trying to match a number, as opposed to a string of digits (like a ZIP code), it'd probably be better to compare numerically rather than with a regex.
Not nice but working:
from 1 to 99999: [1-9][0-9]{0,4}
from 01 to 09999: 0[1-9][0-9]{0,3}
from 001 to 00999: 00[1-9][0-9]{0,2}
from 0001 to 00099: 000[1-9][0-9]{0,1}
from 00001 to 00009: 0000[1-9]
And then putting all this together:
^(0[1-9][0-9]{0,3}|00[1-9][0-9]{0,2}|000[1-9][0-9]{0,1}|0000[1-9]|[1-9][0-9]{0,4})$
Edit: updated.
Does it really need to be a regular expression? Why not just check if 0 < value && value <=99999?
Ok I understand, here the solution :
^[1-9][0-9]{0,4}$
Take all number between 1 and 99999
Why don't you use a RangeValidator it is simplier.
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Value must be greater than zero." ControlToValidate="tbQty" MinimumValue="1" MaximumValue="99999" Type="Integer"></asp:RangeValidator>

Resources