Need Regular Expression for Phone Number in MVC Data Annotation - regex

Need Regular expression for Phone Number like
(123)-123-1212 Valid
(123)-123-121 InValid
(123)-123-12 InValid
1212-344--- Invalid
(000)-123-1212 InValid
Only first format should be valid. Number should be 0-9
I don't have any idea regarding this expression

You can use the following:
^\((?!000)\d{3}\)-\d{3}-\d{4}$
Explanation:
^ match start of the string
\( followed by a parentheses ( (escaped because it has special meaning in regex)
(?!000) negative lookahead (to fail for 000)
\d{3} match a digit exactly three times (\d equivalent to [0-9])
\) close parentheses
- match hyphen literally
\d{3}-\d{4} followed by exactly 3 digits then a hyphen and exactly 4 digits
$ followed by end of the string (so that it wont match strings with other charcters after the specified patten)

Related

Regular expression to Star with number and then numbers or one letter

I have this regular expression
^[0-9]+[a-zA-Z0-9]*
But I need one that always stars with a number then it can be another number or a letter, but it can not be number letter number. The letter will always be the last. Like this example
102A OK
1A OK
2 OK
110 OK
10A1 WRONG
BV WRONG
The letter cannot be between two numbers.
You could use a negative lookahead.
^(?!\d+[a-z]\d)\d.*
with the case-indifferent flag set.
Demo
A match of this regular expression signifies that the string does not contain a 3-character substring consisting of a digit, a letter, a digit, in that order. If the entire string is to be matched when the match is successful, add .* to the end of the regex.
The regex engine performs the following operations.
^ match beginning of line
(?! begin negative lookahead
\d+[a-z]\d match digits-letter-digit
) end negative lookahead
\d match a digit
Note that \d at the end must follow the negative lookahead. If the regex were ^\d(?!.\d+[a-z]\d) and the string were 1A1 the negative lookahead would fail to find digit-letter-digit in A1 and the overall match would succeed (incorrectly).
Because the negative lookahead is pinned to the beginning of the line and consumes no characters, if it fails (match succeeds) the search for \d at the end of the regex begins at the beginning of the line.
You could match a char 1-9 followed by optional digits 0-9 and optional chars a-zA-Z.
If you use [a-zA-Z0-9] the character class will match any of the listed in any order.
If you separate the chars and the digits, the letter can not come before the digits and, as the * quantifier matches 0 or more times, you can also match a single digit.
^[1-9][0-9]*[a-zA-Z]*$
Regex demo

Regular expression not matching complete input

Why is the regular expression ([£€$¥£]|USD|US\$)\s?(\d*.?\d+|\d{1,3}(,\d{3})*(.\d+)?) not matching US$ 150,000.00
Regular expression 1 :
([£€$¥£]|USD|US\$)\s?
matches US$
Regular expression 2 :
(\d*\.?\d+|\d{1,3}(,\d{3})*(\.\d+)?)
matches 150,000.00
Concatenation of two expressions
([£€$¥£]|USD|US\$)\s?(\d*\.?\d+|\d{1,3}(,\d{3})*(\.\d+)?)
does not match US$ 150,000.00
demo : https://regex101.com/r/fJJWqv/1
EDIT : The Regular expression 2 does not match 150,000.00 but shouldn't it match the comma too because of (,\d{3})* ?
Your second claim is untrue. (\d*\.?\d+|\d{1,3}(,\d{3})*(\.\d+)?) does not match 150,000.00. Rather, it matches 150 and 000.00. Since only the former is prefixed with US $, only it matches the third regex.
The reason for this is that the alternation order you specified favors a shorter match. To fix it, you can switch the alternation order: change (\d*\.?\d+|\d{1,3}(,\d{3})*(\.\d+)?) to (\d{1,3}(,\d{3})*(\.\d+)?|\d*\.?\d+).
In 150,000.00 using pattern (\d*\.?\d+|\d{1,3}(,\d{3})*(\.\d+)?) it will not match the comma because the 150 will be matched by \d*\.?\d+ and none of the alternatives start with a comma.
It can because \d* means 0+ digits so that will match 150. Then the \.? is an optional dot so it continues to \d+.
Due to bracktracking the \d* can give up one match to match at least 1 digit from \d+ and 150 will stay the match.
Then the next character is a , but non of the alternations start with a comma so the next character is tried and this time this pattern \d*\.?\d+ can match the 000.00.
One option to match your value (and if you only want the match you can omit the capturing groups) is you remove this part \d*\.?\d+
(?:[£€$¥£]|USD|US\$)\s?\d{1,3}(?:,\d{3})*(?:\.\d+)?
Regex demo

Why is this regex selecting this text

I am using the regex
(.*)\d.txt
on the expression
MyFile23.txt
Now the online tester says that using the above regex the mentioned string would be allowed (selected). My understanding is that it should not be allowed because there are two numeric digits 2 and 3 while the above regex expression has only one numeric digit in it i.e \d.It should have been \d+. My current expression reads. Zero of more of any character followed by one numeric digit followed by .txt. My question is why is the above string passing the regex expression ?
This regex (.*)\d.txt will still match MyFile23.txt because of .* which will match 0 or more of any character (including a digit).
So for the given input: MyFile23.txt here is the breakup:
.* # matches MyFile2
\d # matched 3
. # matches a dot (though it can match anything here due to unescaped dot)
txt # will match literal txt
To make sure it only matches MyFile2.txt you can use:
^\D*\d\.txt$
Where ^ and $ are anchors to match start and end. \D* will match 0 or more non-digit.
The pattern you have has one group (.*) which would match using your example:MyFile2
because the . allows any character.
Furthermore the . in the pattern after this group is not escaped which will result in allowing another character of any kind.
To avoid this use:
(\D*)\d+\.txt
the group (\D*) would now match all non digit characters.
Here is the explanation, your "MyFile23.txt" matches the regex pattern:
A literal period . should always be escaped as \. else it will match "any character".
And finally, (.*) matches all the string from the beginning to the last digit (MyFile2). Have a look at the "MATCH INFORMATION" area on the right at this page.
So, I'd suggest the following fix:
^\D*\d\.txt$ = beginning of a line/string, non-digit character, any number of repetitions, a digit, a literal period, a literal txt, and the end of the string/line (depending on the m switch, which depends on the input string, whether you have a list of words on separate lines, or just a separate file name).
Here is a working example.

Regex to find repeating numbers

Can anyone help me or direct me to build a regex to validate repeating numbers
eg : 11111111, 2222, 99999999999, etc
It should validate for any length.
\b(\d)\1+\b
Explanation:
\b # match word boundary
(\d) # match digit remember it
\1+ # match one or more instances of the previously matched digit
\b # match word boundary
If 1 should also be a valid match (zero repetitions), use a * instead of the +.
If you also want to allow longer repeats (123123123) use
\b(\d+)\1+\b
If the regex should be applied to the entire string (as opposed to finding "repeat-numbers in a longer string), use start- and end-of-line anchors instead of \b:
^(\d)\1+$
Edit: How to match the exact opposite, i. e. a number where not all digits are the same (except if the entire number is simply a digit):
^(\d)(?!\1+$)\d*$
^ # Start of string
(\d) # Match a digit
(?! # Assert that the following doesn't match:
\1+ # one or more repetitions of the previously matched digit
$ # until the end of the string
) # End of lookahead assertion
\d* # Match zero or more digits
$ # until the end of the string
To match a number of repetitions of a single digit, you can write ([0-9])\1*.
This matches [0-9] into a group, then matches 0 or more repetions (\1) of that group.
You can write \1+ to match one or more repetitions.
Use a backreference:
(\d)\1+
Probably you want to use some sort of anchors ^(\d)\1+$ or \b(\d)\1+\b
I used this expression to give me all phone numbers that are all the same digit.
Basically, it means to give 9 repetitions of the original first repetition of a given number, which results in 10 of the same number in a row.
([0-9])\1{9}
(\d)\1+? matches any digit repeating
you can get repeted text or numbers easily by backreference take a look on following example:
this code simply means whatever the pattern inside [] . ([inside pattern]) the \1 will go finding same as inside pattern forward to that.

what does this regular expression mean?

^(?!-)[a-z\d\-]{1,100}$
Here's an explanation using regex comment mode, so this expanded form can itself be used as a regex:
(?x) # flag to enable comment mode
^ # start of line/string.
(?!-) # negative lookahead for literal hyphen (-) character, so fails if the next position contains one.
[a-z\d\-] # character class matches a single alpha (a-z), digit (\d) or hyphen (\-).
{1,100} # match the above [class] upto 100 times, at least once.
$ # end of line/string.
In short, it's matching upto 100 lowercase alphanumerics or hyphen, but the first character must not be hyphen.
Could be attempting to validate a serial number, or similar, but it's too general to say for sure.
Not all regex engines support negative lookaheads. If you're trying to figure out what it is doing in order to adapt for an engine without negative lookaheads, you can use:
^[a-z\d][a-z\d-]{0,99}$
(?!-) == negative lookahead
start of line not followed by a - that contains at least 1 to 100 characters that can be a-z or 0-9 or a - followed by the end of the line, though the \d in the character class is probably wrong and should be specified by 0-9 otherwise the a-z takes care of a 'd' character, depends on the regex flavor.
A string of letters, digits and dashes. Between 1 and 100 characters. The first character is not a dash.