I know this question is asked like a thousand times in here, but I can't get the hang of it yet. I need help with checking a textbox if it matches a Phone Number format. The format should be likes this :
000-000-000 or (+000)00-000-000. Can anybody help me ?
give this pattern a try,
^(\(\+\d{3}\)|\d)\d{2}(-\d{3}){2}$
ScreenShot:
Generated Explanation:
Assert position at the beginning of a line (at beginning of the string or after a line break character) ^
Match the regular expression below and capture its match into backreference number 1 (\(\+\d{3}\)|\d)
Match either the regular expression below (attempting the next alternative only if this one fails) \(\+\d{3}\)
Match the character “(” literally \(
Match the character “+” literally \+
Match a single digit 0..9 \d{3}
Exactly 3 times {3}
Match the character “)” literally \)
Or match regular expression number 2 below (the entire group fails if this one fails to match) \d
Match a single digit 0..9 \d
Match a single digit 0..9 \d{2}
Exactly 2 times {2}
Match the regular expression below and capture its match into backreference number 2 (-\d{3}){2}
Exactly 2 times {2}
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. {2}
Match the character “-” literally -
Match a single digit 0..9 \d{3}
Exactly 3 times {3}
Assert position at the end of a line (at the end of the string or before a line break character) $
Pattern 1 is \d{3}\-\d{3}\-\d{3}
Pattern 2 is \(\+\d{3}\)\d{2}\-d{3}\-\d{3}
So you need to match for Pattern1 OR Pattern2:
(\d{3}\-\d{3}\-\d{3})|(\(\+\d{3}\)\d{2}\-d{3}\-\d{3})
(?:\d|\(\+\d{3}\))\d{2}(?:-\d{3}){2}
Or, if you're regarding of performance, better change it to:
(?:\(\+\d{3}\)|\d)\d{2}(?:-\d{3}){2}
Related
I'm trying to build a regex to check if a text input is valid.
The pattern is [NumberBetween1And999]['x'][NumberBetween1And999][','][White space Optional] repeated infinite times.
I need this to make an order from a string: the first number is the product id and the second number is the quantity for the product.
Examples: of good texts:
1x1
2x1,3x1
1x3, 4x1
Should not catch:
1x1,
1,1, 1x1,
9999x1
1x1,99999x1
I'm blocked there: ^(([1-9][0-9]{0,2})x([1-9][0-9]{0,2}),)*$
Thanks for helping me
You can use
^[1-9][0-9]{0,2}x[1-9][0-9]{0,2}(?:,\s*[1-9][0-9]{0,2}x[1-9][0-9]{0,2})*$
The pattern matches:
^ Start of string
[1-9][0-9]{0,2}x[1-9][0-9]{0,2} Match a digit 1-9 and 2 optional digits 0-9, then x and again the digits part
(?: Non capture group to repeat as a whole
,\s* Match a comma and optional whitespace char
[1-9][0-9]{0,2}x[1-9][0-9]{0,2} Match the same pattern as at the beginning
)* Close the non capture group and optionally repeat it to also match a single part without a comma
$ End of string
Regex demo
I want to match an 8 digit number. Currently, I have the following regex but It is failing in some cases.
(\d+)\1{6}
It matches only when a number is different at the end such as 44444445 or 54444444. However, I am looking to match cases where at least 7 digits are the same regardless of their position.
It is failing in cases like
44454444
44544444
44444544
What modification is needed here?
It's probably a bad idea to use this in a performance-sensitive location, but you can use a capture reference to achieve this.
The Regex you need is as follows:
(\d)(?:.*?\1){6}
Breaking it down:
(\d) Capture group of any single digit
.*? means match any character, zero or more times, lazily
\1 means match the first capture group
We enclose that in a non-capturing group {?:
And add a quantifier {6} to match six times
You can sort the digits before matching
string input = "44444445 54444444 44454444 44544444 44444544";
string[] numbers = input.Split(' ');
foreach (var number in numbers)
{
number = String.Concat(str.OrderBy(c => c));
if (Regex.IsMatch(number, #"(\d+)\1{6}"))
// do something
}
Still not a good idea to use regex for this though
The pattern that you tried (\d+)\1{6} matches 6 of the same digits in a row. If you want to stretch the match over multiple same digits, you have to match optional digits in between.
Note that in .NET \d matches more digits than 0-9 only.
If you want to match only digits 0-9 using C# without matching other characters in between the digits:
([0-9])(?:[0-9]*?\1){6}
The pattern matches:
([0-9]) Capture group 1
(?: Non capture group
[0-9]*?\1 Match optional digits 0-9 and a backreference to group 1
){6} Close non capture group and repeat 6 times
See a .NET Regex demo
If you want to match only 8 digits, you can use a positive lookahead (?= to assert 8 digits and word boundaries \b
\b(?=\d{8}\b)[0-9]*([0-9])(?:[0-9]*?\1){6}\d*\b
See another .NET Regex demo
I tried this expression:
/([a-z]+[0-9]+[a-z]*){1,5}$/
but it's works for every word that start with letter and contains at least one number and more then two symbol for example "re1111e" when its not supposed to, what am I doing wrong?
One possible way to write your regex uses a positive lookahead to check for a number:
/(?=[^0-9]*[0-9])[a-z][a-z0-9]{0,4}/
This pattern says to:
(?=[^0-9]*[0-9]) assert that a single digit appears somewhere
[a-z] match an initial letter character
[a-z0-9]{0,4} then match zero to four letter or number characters
In your pattern, the quantifier {1,5} apllies to the group repeating this match [a-z]+[0-9]+[a-z]* 1 - 5 times.
If I am not mistaken, you want to match [a-z] from the start followed by 4 chars from which one of them is at least 1 digit so the minimum amount of characters is 2 and the maximum is 5.
You might use:
^(?=.{2,5}$)[a-z][a-z0-9]*[0-9][a-z0-9]*$
About the pattern
^ Start of string
(?=.{2,5}$ Assert string length 2 - 5 characters
[a-z] Match a-z
[a-z0-9]* Repeat 0+ times matching a-z 0-9
[0-9] Match a digit
[a-z0-9]* Repeat 0+ times matching a-z 0-9
$ End of string
Regex demo
How do i write regex statement for only numbers between 0 and 255? 0 and 255 will be valid for the statement.
You can find some numeric ranges here:
http://www.regular-expressions.info/numericranges.html
Your example would be:
^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$
^([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
This tool is quite helpful for such things. A little searching doesn't hurt anyone either.
If you want to allow leading zeroes the pattern needs to be adapted, though. E.g.:
^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$
Try a negative look behind:
(?<!\-)\b0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b
Explanation
<!--
(?<!\-)\b0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b
Options: ^ and $ match at line breaks
Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!\-)»
Match the character “-” literally «\-»
Assert position at a word boundary «\b»
Match the character “0” literally «0*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below and capture its match into backreference number 1 «([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])»
Match either the regular expression below (attempting the next alternative only if this one fails) «[0-9]{1,2}»
Match a single character in the range between “0” and “9” «[0-9]{1,2}»
Between one and 2 times, as many times as possible, giving back as needed (greedy) «{1,2}»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «1[0-9]{2}»
Match the character “1” literally «1»
Match a single character in the range between “0” and “9” «[0-9]{2}»
Exactly 2 times «{2}»
Or match regular expression number 3 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
Match the character “2” literally «2»
Match a single character in the range between “0” and “4” «[0-4]»
Match a single character in the range between “0” and “9” «[0-9]»
Or match regular expression number 4 below (the entire group fails if this one fails to match) «25[0-5]»
Match the characters “25” literally «25»
Match a single character in the range between “0” and “5” «[0-5]»
Assert position at a word boundary «\b»
-->
Try
([01]?\d\d?|2[0-4]\d|25[0-5])
I'm trying to use a .NET Regex to validate the input format of a string. The string can be of the format
single digit 0-9 followed by
single letter A-Z OR 07 OR 03 or AA followed by
two letters A-Z
So 0AAA, 107ZF, 503GH, 0AAAA are all valid. The string with which I construct my Regex is as follows:
"([0-9]{1})" +
"((03$)|(07$)|(AA$)|[A-Z]{1})" +
"([A-Z]{2})"
Yet this does not validate strings in which the second term is one of 03, 07 or AA. Whilst debugging, I removed the third term from the string used to construct the regex, and found that input strings of the form 103, 507, 6AA WOULD validate.......
Any ideas why, when I then put the third term back into the Regex, the input strings such as 1AAGM do not match?
Thanks
Tom
This is because your expression requires the strings with 03, 07 and AA to end right there ($ matches the end of input). Remove the $ from these sub-expressions, and move it to the end of the expression.
"^[0-9](03|07|AA|[A-Z])[A-Z]{2}$"
I believe this is because you are using the "$" in the regex, which means in this case to assert position at the end of a line (at the end of the string or before a line break character). Remove it and it should work. From Regex Buddy, here is what you were doing:
([0-9]{1})((03$)|(07$)|(AA$)|[A-Z]{1})([A-Z]{2})
Options: ^ and $ match at line breaks
Match the regular expression below and capture its match into backreference number 1 «([0-9]{1})»
Match a single character in the range between “0” and “9” «[0-9]{1}»
Exactly 1 times «{1}»
Match the regular expression below and capture its match into backreference number 2 «((03$)|(07$)|(AA$)|[A-Z]{1})»
Match either the regular expression below (attempting the next alternative only if this one fails) «(03$)»
Match the regular expression below and capture its match into backreference number 3 «(03$)»
Match the characters “03” literally «03»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «(07$)»
Match the regular expression below and capture its match into backreference number 4 «(07$)»
Match the characters “07” literally «07»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
Or match regular expression number 3 below (attempting the next alternative only if this one fails) «(AA$)»
Match the regular expression below and capture its match into backreference number 5 «(AA$)»
Match the characters “AA” literally «AA»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
Or match regular expression number 4 below (the entire group fails if this one fails to match) «[A-Z]{1}»
Match a single character in the range between “A” and “Z” «[A-Z]{1}»
Exactly 1 times «{1}»
Match the regular expression below and capture its match into backreference number 6 «([A-Z]{2})»
Match a single character in the range between “A” and “Z” «[A-Z]{2}»
Exactly 2 times «{2}»
Followed by the revised version:
([0-9]{1})((03)|(07)|(AA)|[A-Z]{1})([A-Z]{2})
Options: ^ and $ match at line breaks
Match the regular expression below and capture its match into backreference number 1 «([0-9]{1})»
Match a single character in the range between “0” and “9” «[0-9]{1}»
Exactly 1 times «{1}»
Match the regular expression below and capture its match into backreference number 2 «((03)|(07)|(AA)|[A-Z]{1})»
Match either the regular expression below (attempting the next alternative only if this one fails) «(03)»
Match the regular expression below and capture its match into backreference number 3 «(03)»
Match the characters “03” literally «03»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «(07)»
Match the regular expression below and capture its match into backreference number 4 «(07)»
Match the characters “07” literally «07»
Or match regular expression number 3 below (attempting the next alternative only if this one fails) «(AA)»
Match the regular expression below and capture its match into backreference number 5 «(AA)»
Match the characters “AA” literally «AA»
Or match regular expression number 4 below (the entire group fails if this one fails to match) «[A-Z]{1}»
Match a single character in the range between “A” and “Z” «[A-Z]{1}»
Exactly 1 times «{1}»
Match the regular expression below and capture its match into backreference number 6 «([A-Z]{2})»
Match a single character in the range between “A” and “Z” «[A-Z]{2}»
Exactly 2 times «{2}»