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])
Related
I want to match strings that do not contain more than 3 of the same character repeated in a row. So:
abaaaa [no match]
abawdasd [match]
abbbbasda [no match]
bbabbabba [match]
Yes, it would be much easier and neater to do a regex match for containing the consecutive characters, and then negate that in the code afterwards. However, in this case that is not possible.
I would like to open out the question to x consecutive characters so that it can be extended to the general case to make the question and answer more useful.
Negative lookahead is supported in this case.
Use a negative lookahead with back references:
^(?:(.)(?!\1\1))*$
See live demo using your examples.
(.) captures each character in group 1 and the negative look ahead asserts that the next 2 chars are not repeats of the captured character.
To match strings not containing a character repeated more than 3 times consecutively:
^((.)\2?(?!\2\2))+$
How it works:
^ Start of string
(
(.) Match any character (not a new line) and store it for back reference.
\2? Optionally match one more exact copies of that character.
(?! Make sure the upcoming character(s) is/are not the same character.
\2\2 Repeat '\2' for as many times as you need
)
)+ Do ad nauseam
$ End of string
So, the number of /2 in your whole expression will be the number of times you allow a character to be repeated consecutively, any more and you won't get a match.
E.g.
^((.)\2?(?!\2\2\2))+$ will match all strings that don't repeat a character more than 4 times in a row.
^((.)\2?(?!\2\2\2\2))+$ will match all strings that don't repeat a character more than 5 times in a row.
Please be aware this solution uses negative lookahead, but not all not all regex flavors support it.
I'm answering this question :
Is there a regular expression for matching a string that has no more than 2 repeating characters?
which was marked as an exact duplicate of this question.
Its much quicker to negate the match instead
if (!Regex.Match("hello world", #"(.)\1{2}").Success) Console.WriteLine("No dups");
I am currently working on a regex which needs to match exactly 8 digits. But sometimes it occurs that there are spaces or dots between those numbers. This is the regex that i am currently using.
([0-9\ ?.?]{7,16})
It works fine most of the time, but the problem I am having is that it sometimes matches number with a lot of spaces tailing it so you will get something like 1234/s/s/s/s (/s stands for space) Or sometimes it is only matching spaces.
What i want is a regex that always matches at least 8 digits and also allows spaces and dots without detecting less then 8 digits. I know it may be stupid question, but I couldn't find anything I need elswhere.
Your ([0-9\ ?.?]{7,16}) expression matches 7 to 16 occurrences of any character that is either a digit, or a space, or a ?, or .. Yes, the ? inside [...] is a literal ?, not a quantifier.
You need to use an expression that will match a digit ([0-9]) and then exactly 7 sequences of a space or period ([ .]) followed with 1 digit, and to make sure you are not matching the digits in 123.156.78.146 you may use special boundaries:
(?<!\d[ .]?)\d(?:[. ]?\d){7}(?![ .]?\d)
if the space or . can only be 0 to 1 in between digits; or - if the space/dot can appear 0 or more times,
(?<!\d[ .]*)\d(?:[. ]*\d){7}(?![ .]*\d)
See the regex demo
The (?<!\d[ .]*) is a negative lookbehind that will fail any match if it starts with a digit that is followed with .(s) or space(s), and the (?![ .]*\d) negative lookahead will fail the match if the 7 digits you need are followed with .(s) or space(s) and a digit.
To solve this, describe the problem to yourself. You want to match one digit followed by seven repetitions of space-or-dot followed by a digit. This leads to a regular expression like \d([ .]?\d){7}. To avoid collecting the seven captures add a :? after the (. To capture the whole string, enclose it in brackets. Adding both changes gives the expression (\d(:?[ .]?\d){7}). If more than one space or dot is allowed between the digits then change the ? to a *.
To get just the eight digits out of the string I suggest using the string captured above and replacing any spaces or dots with nothing.
I'm want to use a Regex to split long string for seperated lines.
Line can include any possible unicode character.
Line is "ending" on dot ("." - one or more) or on new line ("\n").
Example:
This string will be the input:
"line1. line2.. line3... line4.... line5..... line6
\n
line7"
The output:
"line1."
"line2.."
"line3..."
"line4...."
"line5....."
"line6"
"line7"
If I understand what you're asking for, you might try a pattern like this:
(?<=\.)(?!\.)|\n
This will split the string on any position which is preceded by a . but not followed by a . or a \n character.
Note that this pattern preserves any whitespace after the dots, for example:
var input = #"line1. line2.. line3... line4.... line5..... line6\nline7";
var output = Regex.Split(input, #"(?<=\.)(?!\.)|\n");
Produces
line1.
line2..
line3...
line4....
line5.....
line6
line7
If you'd like to get rid of the whitespace simply change this to:
(?<=\.)(?!\.)\s*|\n
But if you know that the dots will always be followed by whitespace, you can simplify this to:
(?<=\.)\s+|\n
Try this:
String result = Regex.Replace(subject, #"""?(\w+([.]+)?)(?:[\n ]|[""\n]$)+", #"""$1""\n");
/*
"line1."
"line2.."
"line3..."
"line4...."
"line5....."
"line6"
"line7"
*/
Regex Explanation
"?(\w+([.]+)?)(?:[\n ]|["\n]$)+
Match the character “"” literally «"?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the regular expression below and capture its match into backreference number 1 «(\w+([.]+)?)»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one 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 2 «([.]+)?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the character “.” «[.]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below «(?:[\n ]|["\n]$)+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match either the regular expression below (attempting the next alternative only if this one fails) «[\n ]»
Match a single character present in the list below «[\n ]»
A line feed character «\n»
The character “ ” « »
Or match regular expression number 2 below (the entire group fails if this one fails to match) «["\n]$»
Match a single character present in the list below «["\n]»
The character “"” «"»
A line feed character «\n»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
If you want to keep all dots intact and dots will be followed by a empty space, then this could be your regex:
String result = Regex.Replace(t, #".\s", #".\n");
This will be one string. You haven't stated if you want more strings or one as result.
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}
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}»