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
Related
I currently have a regex validator to restrict the user to only input numbers greater than 1. How can I allow both positive and negative numbers?
^[1-9]+([0-9]+)*$
Adding -? will do the trick:
^-?[1-9]+([0-9]+)*$
Assuming a negative number will be simply marked by a preceding - sign the following expression should work:
/(^|( )|\t)(-|)\d{1,}/gm
Explanation:
First, (^| ) matches a start of a new line OR (because of the |) a white space (from a space bar) OR a tab. If you have requirements on the white space surrounding the input, you can tweak this regular expression in this section.
Next, (-|) matches either the - character OR (because of the |) nothing
Then it matches a digit \d, where there is at least 1 digit, but possibly an infinite number (because of {1,})
Next, the g sets the global flag allowing more than one instance to be matched.
Finally, the m sets the multi-line flag, allowing matches to span across lines. This is necessary for the ^ new-line character to match properly.
This was tested with the following cases:
-0934 sdj2a
1328 232
-93 2939 -192
Where the matched groups were:
-0934,1328, 232, -93, 2939, -192
i want to make the regex for allowing phone number from 7 to 26 digits but except 8 and 9 digit.
I tried is this one but its not working :
^\d(?:[-\s]?\d){6,}((?!\d(?:[-\s]?\d){7}).)((?!\d(?:[-\s]?\d){8}).)((?!\d(?:[-\s]?\d){9,26}).)$
Allowed Input : Numeric 7 to 26 (but not 8 or 9 digits)
1234567
1234567890
Not allowed input :
12
123
1234
123456
12345678
L123456789
I guess this regex will do the job:
^(\d{7}|\d{10,26})$
Try the demo
Your regex allows for or a space or hyphen between any pair of digits, is that a requirement? Assuming it is, here's what I would use:
^\d(?!(?:[-\s]?\d){7,8}$)(?:[-\s]?\d){6,25}$
I think the biggest problem with your regex is the placement of the lookaheads. They have to be used at the beginning of the regex to do any good. What's happening in your regex is that the \d(?:[-\s]?\d){6,} consumes all the digits it can, then the lookaheads are applied at the end of the string, where there are no more characters of any kind. So, being negative lookaheads, they always succeed.
Another problem is the dot (.) following each lookahead; because they're not inside the lookaheads, and the enclosing groups are not optional, each dot has to consume one character, which is not accounted for by any of the range quantifiers (especially the last one, {9,26}), and is not limited to matching just a digit, hyphen or space.
It looks like you're trying to use the captive lookahead idiom (as I call it) demonstrated in this answer. It's not useful in this case.
You can use:
\b(\d{7}(?:\d{3,19})?)\b
RegEx Demo
I couldn't find a precise definition of legal syntax for CSS3 colors, either as regular expression, BNF or whatever strict formal definition there might be. Some info can be derived from the verbal description in the CSS3 Color Module (for example that comma separated lists may contain whitespace), but I don't see whether e.g. leading zeros in something like
rgb(010,005,255)
rgba(050%,1%,01%,0)
are actually legal, or omitting leading zeros of decimal fractions, like
rgba(100,100,100,.5)
I'm not talking about what is tolerated by browsers, I'm asking whether this is officially legal CSS3 syntax as I'm interested in the use of these color definitions in non-browser applications as well.
As you found already, the CSS3 Color Module specification says
The format of an RGB value in the functional notation is 'rgb(' followed by a comma-separated list of three numerical values (either three integer values or three percentage values) followed by ')'
But you then need to look in basic data types section of CSS 2.1 to find out what an integer or a percentage value is and it says...
Some value types may have integer values (denoted by ) or real number values (denoted by ). Real numbers and integers are specified in decimal notation only. An consists of one or more digits "0" to "9". A can either be an , or it can be zero or more digits followed by a dot (.) followed by one or more digits. Both integers and real numbers may be preceded by a "-" or "+" to indicate the sign. -0 is equivalent to 0 and is not a negative number.
So integers and numbers can have leading zeros.
Then later on basic data types says
The format of a percentage value (denoted by in this specification) is a immediately followed by '%'.
So percentages can have leading zeros too.
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
Can some please help me with regular expression for height in cm ( eg. 170.25)(after dot only 2 character), weight in kg ( eg. 57.750) (after dot only 3 character),both numeric.
this kind of value format should be accepted
Height: 57,57.55 or 150,150.55
Weight: 77,77.55,77.565 or 150,150.77,150.777
\d+(\.\d{1,3})?
should work
The basic regular expressions are quite straight forward:
\d{2,3}\.\d{,2}
will match the height (any number of decimal digits followed by a decimal point followed by exactly 2 digits and:
\d{2,3}\.\d{,3}
will match the weight. Having said that, depending on where the input comes from they will match other things too (e.g. bits of ip addresses) so I would add more context to the expression. You should also check how exact weights are represented. Is 57 kg shown as 57, 57.0, or 57.000 (the expression above will only match the latter.