Regex: find a variable number sequence - regex

I am looking for a specific sequence of numbers in a line. I can best explain with an example:
00001 # first search criteria - line 1
00010 # second search criteria - line 2
So every line has 5 digits of either 0 or 1. I am looking for the combination of all 0 except for 1 digit that can be a 1. This 1 can be in any position of the 5 digits.
The regex code I have for 5 digits of 0 is
^((0\s*?){5}) # there may be spaces between the numbers
The line 1 case above would be selected with following regex code:
^((0\s*?){4})\s*(1)
My question is how I could write in regex code the changing position of 1 to cover the 5 cases/positions.
Thank you.

You can use a lookahead based regex for this:
^(?=[0\s]*1[0\s]*$)(?:\s*[01]\s*){5}$
RegEx Demo
Lookahead (?=[0\s]*1[0\s]*$) will enforce only single 1 at any position in input where as (?:\s*[01]\s*){5} will make sure that input has only 0 and 1 with 5 digits length also allowing white-spaces anywhere.

You can use two conditionals.
First one insures 1 is not found again.
Second one insures 1 is found.
(?:((?(1)(?!))1)|0){5}(?(1)|(?!))
Expanded
(?:
( # (1 start)
(?(1) (?!) ) 1
) # (1 end)
| 0
){5}
(?(1) | (?!) )

Related

Checking min no of characters in capturing group in Regex

I have a question in regex
I am dealing with numbers 0 and 1 only
I have 10 digit number grouped into 4 as below
([01]{2})([01]{4})([01]{2})([01]{2})
I need to match all those numbers with min 2 1's in the second group which is ([01]{4}) , no matter how many 0's or 1's other groups are having. I am interested only in the second group
For example, these are the potential matches are
0000110000
0011000000
0001100000
0000110000
I tried using positive look ahead like :
^(\d{2})((?=\d*1{2,}\d*)(\d{4}))(\d{2})(\d{2})
but this is matching even
0000000011
Any help is deeply appreciated
If the two 1s are not necessarily consecutive in Group 2, you can use
^([01]{2})(?=(?:[01]*1){2}[01]{4,6}$)([01]{4})([01]{2})([01]{2})$
See the regex demo
Details:
^ - start of string
([01]{2}) - Group 1: two occurrences of 1 or 0
(?=(?:[01]*1){2}[01]{4,6}$) - immediately to the right of the current location, there must be two occurrences of any zero or more 0 or 1 chars followed with 1 and then there must be four, five or six 1 or 0 chars till the end of string
([01]{4}) - Group 2: four occurrences of 1 or 0
([01]{2}) - Group 3: two occurrences of 1 or 0
([01]{2}) - Group 4: two occurrences of 1 or 0
$ - end of string.
If the ones need to be consecutive (as per your sample data), maybe you can use:
^(?=[01]{2,4}11)[01]{10}$
See the online demo. The idea here is that you would match 2-4 zero's or 1's upto a sequence of two ones. It makes sense if you realise the only combinations that are allowed would have the minimum of two 1's ("11") sequence after exactly 2-4 other digits.
^ - Start line anchor.
(?=[01]{2,4}11) - Open positive lookahead to look for 2-4 characters from our characters class upto "11".
[01]{10} - Match exactly 10 characters from our character class.
$ - End line anchor.
If need be you can change the [01]{10} pieces where you'd use capture groups.
EDIT:
If they don't have to be consecutive, maybe you can work with:
^[01]{2}(?=[01]{8}$)([01]{0,2}1[01]{0,2}1[01]{0,2})[01]{4}$
See the online demo.
Or less verbose:
^(?=[01]{10}$)(..)(.*1.*1.*)(..)(..)$
See the demo
Not a job for regex but for bitwise operators:
(in PHP):
$nums = [
'0000110000',
'0011000000',
'0001100000',
'1000110000',
'0000000110',
'0001000000'
];
foreach ($nums as $num) {
if ( !in_array((bindec($num) >> 4) & 15, [0, 1, 2, 4, 8]) )
echo $num, PHP_EOL;
}
You can probably do that in any language.
If a positive lookahead is supported, you could also assert that group 2 has as least 11 using a positive lookahead.
^([01]{2})(?=[01]{0,2}11)([01]{4})([01]{2})([01]{2})$
^ Start of string
([01]{2}) - Group 1: two occurrences of 1 or 0
(?= Positive lookahead
[01]{0,2}11 Match 0-2 times either 0 or 1 and match 11
) Close lookahead
([01]{4}) - Group 2: four occurrences of 1 or 0
([01]{2}) - Group 3: two occurrences of 1 or 0
([01]{2}) - Group 4: two occurrences of 1 or 0
$ - end of string.
Regex demo
Or you can write out all 3 alternatives matching 11
^([01]{2})(11[01][01]|[01]11[01]|[01][01]11)([01]{2})([01]{2})$
Regex demo

regex input validation for mobile number 03025498448 using C#

I am trying to do regex validation for 11 digit mobile number of type 03025398448.Where first 3 digits are constant 030 and remaining 8 digits are from 0 to 9 (any number) and 1st digit could be written in +92 format .So, help me for this number regex code
If the number should start with 030 and +92 is optional and when using +92 you should omit the leading zero, you could use:
^(?:\+9230|030)?\d{8}$
Explanation
^ # From the beginning of the string
(?: # Non capturing group
\+9230|030 # Match +9230 or 030
)? # close capturing group and make it optional
\d{8} # Match 8 digits
$ # The end of the string
In C# you could use this as string pattern = #"^(?:\+9230|030)?\d{8}$";
C# code
You can use this regular expression:
^((\+?92)30[0-9]{8}|030[0-9]{8})$
Explanation
BeginOfLine
CapturingGroup
GroupNumber:1
OR: match either of the followings
Sequence: match all of the followings in order
CapturingGroup
GroupNumber:2
Sequence: match all of the followings in order
Repeat
+
optional
9 2
3 0
Repeat
AnyCharIn[ 0 to 9]
8 times
Sequence: match all of the followings in order
0 3 0
Repeat
AnyCharIn[ 0 to 9]
8 times
EndOfLine

Regex max number 11 and must 2 digits

I have a validation expression i'm trying to figure out. First, I want the user to only be allowed to enter the max number of 11...not 11 characters but the number allow is the max that can be entered. I got that to work with the code below and works fine.
ValidationExpression="^([1-9]|[0-1][0-1])$"
However, I want the user to also be forced to use 2 digits. For example, instead of 1 they need to enter 01. I've tried different ways of doing this but can't seem to get it to work.
I tried this as well but that didn't work either.
ValidationExpression="^([1-9]|[0-1][0-1])${2}"
If you need to perform this in a single step (i.e. you can't do a < and > check as well as a regex) then this should do it:
ValidationExpression="^(?:0\d|1[01])$"
Or, if your language doesn't recognise the \d symbol:
ValidationExpression="^(?:0[0-9]|1[01])$"
"Match either (0 followed by any digit) or (1 followed by 0 or 1), anchored at the beginning and end of the input string."
To match padded 2-digit numbers from 01 to 12 you may use
ValidationExpression="^(0[1-9]|1[01])$"
See the regex demo.
The expression matches:
^
( - start of a group (here, a capturing group is used for better readability, a non-capturing one can also be used)
0 - zero
[1-9] - 1 to 9 digit
| - or
1 - 1
[01] - 0 or 1 digit
) - end of group
$ - end of string.
You can use this regex
/\b(?:[0][\d]|[1][01])\b/
This says enter a number 0 followed by 0-9 or enter 1 followed by 0 or 1. It is bounded on both sides by word boundaries and it is a non-capturing group. Try it out here.

Regex pattern for validation where 2 sections vary in length interdependently

I'm trying to workout a Regex pattern for validating a string that consists of 2 parts that vary in length but the overall length remains the same.
Overall length = 7
start section alpha characters only 1-3 characters
end section 4-6 digits
combinations 1 Alpha + 6 digits or 2 Alpha + 5 digits or 3 Alpha + 4 digits.
In the second and third option the first character is allowed to be a space.
What I have so far is ^(?:([\sA-Z][A-Z]{2})(\d{4})|[\sA-Z]A-Z|A-Z)$
Can that be simplified?
How can I have and optional Alpha character at the end?
You need a look ahead to assert the overall length and a negative look ahead to prevent the "space digit" start:
^(?=.{7}$)(?! \d) ?[a-zA-Z]{1,3}\d{4,6}$
See a live demo with several edge cases.
This might work
# (?i)^(?=.{7}$)(?:[a-z]{1,3}|[ ][a-z]{2,3})\d{4,6}[a-z]?$
(?i) # Case independent
^ # BOL
(?= .{7} $ ) # 7 chars total
(?:
[a-z]{1,3} # 1 to 3 alpha
|
[ ] [a-z]{2,3} # or, space plus 2 to 3 alpha
)
\d{4,6} # 4 to 6 digits
[a-z]? # optional alpha char
$ # EOL

Regular expression for phone numbers

I'm trying:
\d{3}|\d{11}|\d{11}-\d{1}
to match three-digit numbers, eleven-digit numbers, eleven-digit followed by a hyphen, followed by one digit.
But, it only matches three digit numbers!
I also tried \d{3}|\d{11}|\d{11}-\d{1} but doesn't work.
Any ideas?
There are many ways of punctuating phone numbers. Why don't you remove everything but the digits and check the length?
Note that there are several ways of indicating "extension":
+1 212 555 1212 ext.35
If the first part of an alternation matches, then the regex engine doesn't even try the second part.
Presuming you want to match only three-digit, 11 digit, or 11 digit hyphen 1 digit numbers, then you can use lookarounds to ensure that the preceding and following characters aren't digits.
(?<!\d)(\d{3}|\d{11}|\d{11}-\d{1})(?!\d)
\d{7}+\d{4} will select an eleven digit number. I could not get \d{11} to actually work.
This should work: /(?:^|(?<=\D))(\d{3}|\d{11}|\d{11}-\d{1})(?:$|(?=\D))/
or combined /(?:^|(?<!\d))(\d{3}|\d{11}(?:-\d{1})?)(?:$|(?![\d-]))/
expanded:
/ (?:^ | (?<!\d)) # either start of string or not a digit before us
( # capture grp 1
\d{3} # a 3 digit number
| # or
\d{11} # a 11 digit number
(?:-\d{1})? # optional '-' pluss 1 digit number
) # end capture grp 1
(?:$ | (?![\d-])) # either end of string or not a digit nor '-' after us
/