Simple integer regular expression - asp.net

I have ValidationRegularExpression="[0-9]" which only allows a single character. How do I make it allow between (and including) 1 and 7 digits? I tried [0-9]{1-7} but it didn't work.

You got the syntax almost correct: [0-9]{1,7}.
You can make your solution a bit more elegant (and culture-sensitive) by replacing [0-9] with the generic character group "decimal digit": \d (remember that other languages might use different characters for digits than 0-9).
And here's the documentation for future reference:
.NET Framework Regular Expressions

If you want to avoid leading zeros, you can use this:
^(?!0\d)\d{1,7}$
The first part is a negative lookahead assertion, that checks if there is a 0 followed by a number in the string. If so no match.
Check online here: http://regexr.com?2thtr

Related

How to match a minimum number of regex groups or assertions?

OK regex nerds!
I am using regex lookahead assertions for password validation that is similar to the pattern described here:
\A(?=\w{6,10}\z)(?=[^a-z]*[a-z])(?=(?:[^A-Z]*[A-Z]){3})(?=\D*\d)
However, we want to only require that any 3 of the 4 assertions be valid - not necessarily all of them. Any thoughts on how this could be done?
To shorten any kind of pattern, factorize:
\A(?:
(?=\w{6,10}\z) (?=.*[a-z]) (?: (?:.*[A-Z]){3} | .*\d )
|
(?=.*\d) (?=(?:.*[A-Z]){3}) (?: .*[a-z] | \w{6,10}\z )
)
Note that you don't need a lookahead to test the last condition.
demo
Other way, where each condition is optional and that uses a named group to count (.net only):
\A
(?<c>(?=\w{6,10}\z))?
(?<c>(?=[^a-z]*[a-z]))?
(?<c>(?=(?:[^A-Z]*[A-Z]){3}))?
(?<c>(?=\D*\d))?
(?<-c>){3} # decrement c 3 times
(?(c)|(?!$)) # conditional: force the pattern to fail if too few conditions succeed.
demo
There's no "easy" way to do this in a single regular expression. The only way would be to define all possible permutations of the "three out of four" assertions - e.g.
\A(?=\w{6,10}\z)(?=[^a-z]*[a-z])(?=(?:[^A-Z]*[A-Z]){3})| # Maybe no digit
\A(?=[^a-z]*[a-z])(?=(?:[^A-Z]*[A-Z]){3})(?=\D*\d)| # Maybe wrong length
\A(?=\w{6,10}\z)(?=(?:[^A-Z]*[A-Z]){3})(?=\D*\d)| # Maybe no lower
\A(?=\w{6,10}\z)(?=[^a-z]*[a-z])(?=\D*\d) # Maybe not enough uppers
However, this mind-melting regex is clearly not a good solution.
A better approach would be to perform the four checks separately (with regex or otherwise), and count that there is at least three passed conditions.
...However, let's take a step back here and ask: Why are you doing this?? You're implementing a password entropy check. Based on your fuzzy rules, the following passwords are valid:
AAAa1
password1
LETmein
And the following passwords are invalid:
reallylongsecurepassword8374235359232
HorseBatteryStapleCorrect
I would strongly advise against such a bizarrely restrictive policy.
Brief
The easiest method would be to have separate regular expressions and check whether 3/4 of them are successful in your code's language. The only way to do this in regex is to present all cases. That being said, this is probably the easiest method (in regex) to present all options as it allows you to edit the patterns in one location (where they are defined) rather than multiple times (more prone to bugs). The DEFINE constructs in regex are seldom supported, but PCRE regex does.
You can also have your code generate each regex permutation. See this question about generating all permutations of a list in python
I don't know why you want to do this for passwords, it's considered malpractice, but, since you're asking for it, I figured I'd give you the easiest solution possible in regex... You really should only check minimum length (and complexity if you want [based on algorithms] to show the user how secure your system finds their password to be).
Code
(?(DEFINE)
(?<w>(?=\w{6,10}\z))
(?<l>(?=[^a-z]*[a-z]))
(?<u>(?=(?:[^A-Z]*[A-Z]){3}))
(?<d>(?=\D*\d))
)
\A(?:
(?&w)(?&l)(?&u)|
(?&w)(?&l)(?&d)|
(?&w)(?&u)(?&d)|
(?&l)(?&u)(?&d)
)
Note: The regex above uses the x modifier (ignore whitespace) so that we can nicely organize the content.

Regex for digits from 0 to 16 digits Only in ASP.net

I have a text field that is the user choose to enter data in it, it must be digits and must be less than or equal to 16 digits (Not more than that).
I have regular expression for digits,
ValidationExpression="^\d{10}$"
which is be exactly 10 digits. But how to modify it for my scenario?
I am embarrassed by such a simple question, but I got confused. If it is a duplicate question or somebody asked similar question, please let me know.
Note: I need the regex for ASP.net
Here is a good tutorial.
^\d{0,16}$
Note, that in .NET, \d can also match any Unicode character that represents a digit (see here). If you really just want the ASCII digits, use
^[0-9]{0,16}$
(Since I am not sure how you would set RegexOptions if that is possible at all in your case.)
The curly-brace quantifier is {min,max}, so {0,16} will do for your case.

What does this regular expression mean: (?![#$])

Can someone give me an example/explanation what this regular expression does:
(?![#$])
This is part of <%(?![#$])(([^%]*)%)*?> which is what ASP.NET uses to parse server-side code blocks. I understand the second part of the expression but not the first.
I checked the documentation and found (?! ...) means a zero-width negative lookahead but I'm not entirely sure I understand what that means. Any input I tried so far that looks like <% ... %> seems to work - I wonder why this first sub-expression is even there.
Edit:
I came up with this expression for picking up ASP.NET expressions: <%.+?%> then I found the one Microsoft made (the above full expression in question). I'm trying to understand why they chose that particular expression when mine seems a lot simpler. (I'm trying to see if my expression ignores certain boundary conditions that the MS one doesn't.)
It's a negative lookahead assertion that matches if the next character is not # or $, but doesn't consume it.
It's very simlar to the negative character class [^#$] except that the negative character class also consumes the character, preventing it from being matched by the rest of the expression.
To see the difference consider matching <%test%>.
The expression <%(?![#$])(([^%]*)%)*?> captures test%. (rubular)
The expression <%[^#$](([^%]*)%)*?> captures est% because the t was consumed by the negative character class. (rubular)

Regexpression asp.net validator for a few words

I'm trying to create a validator for a string, that may contain 1-N words, which a separated with 1 whitespace (spaces only between words). I'm a newbie in a regex, so I feel a bit confused, cause my expression seem to be correct:
^[[a-zA-Z]+\s{1}]{0,}[a-zA-Z]+$
What am I doing wrong here? (it accepts only 2 words .. but I want it to accept 1+ words)
Any help is greatly appreciated :)
As often happens with someone beginning a new programming language or syntax, you're close, but not quite! The ^ and $ anchors are being used correctly, and the character classes [a-zA-Z] will match only letters (sounds right to me), but your repetition is a little off, and your grouping is not what you think it is - which is your primary problem.
^[[a-zA-Z]+\s{1}]{0,}[a-zA-Z]+$
^ ^^^^^^^^
a bbbacccc
It only matches two words because you effectively don't have any group repetition; this is because you don't really have any groups - only character classes. The simplest fix is to change the first [ and its matching end brace (marked by a's in the listing above) to parentheses:
^([a-zA-Z]+\s{1}){0,}[a-zA-Z]+$
This single change will make it work the way you expect! However, there a few recommendations and considerations I'd like to make.
First, for readability and code maintenance, use the single character repetition operators instead of repetition braces wherever possible. * repeats zero or more times, + repeats one or more times, and ? repeats 0 or one times (AKA optional). Your repetition curly braces are syntactically correct, and do what you intend them to, but one (marked by b's above) should be removed because it is redundant, and the other (marked by c's above) should be shortened to an asterisk *, as they have exactly the same meaning:
^([a-zA-Z]+\s)*[a-zA-z]+$
Second, I would recommend considering (depending upon your application requirements) the \w shorthand character class instead of the [a-zA-Z] character class, with the following considerations:
it matches both upper and lowercase letters
it does match more than letters (it matches digits 0-9 and the underscore as well)
it can often be configured to match non-English (unicode) letters for multi-lingual input
If any of these are unnecessary or undesirable, then you're on the right track!
On a side note, the character combination \b is a word-boundary assertion and is not needed for your case, as you will already begin and end where there are letters and letters only!
As for learning more about regular expressions, I would recommend Regular-Expressions.info, which has a wealth of info about regexes and the inner workings and quirks of the various implementations. I also use a tool called RegexBuddy to test and debug expressions.

Regular expression to match 10-14 digits

I am using regular expressions for matching only digits, minimum 10 digits, maximum 14. I tried:
^[0-9]
I'd give:
^\d{10,14}$
a shot.
I also like to offer extra solutions for RE engines that don't support all that PCRE stuff so, in a pinch, you could use:
^[0-9]{10,14}$
If you're RE engine is so primitive that it doesn't even allow specific repetitions, you'd have to revert to either some ugly hack like fully specifying the number of digits with alternate REs for 10 to 14 or, easier, just checking for:
^[0-9]*$
and ensuring the length was between 10 and 14.
But that won't be needed for this case (ASP.NET).
^\d{10,14}$
regular-expressions.info
Character Classes or Character Sets
\d is short for [0-9]
Limiting Repetition
The syntax is {min,max}, where min is a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches.
The limited repetition syntax also allows these:
^\d{10,}$ // match at least 10 digits
^\d{13}$ // match exactly 13 digits
try this
#"^\d{10,14}$"
\d - matches a character that is a digit
This will help you
If I understand your question correctly, this should work:
\d{10,14}
Note:
As noted in the other answer.. ^\d{10,14}$ to match the entire input

Resources