I've got a question that asks for a non-empty string that starts and ends with two 1's. The alphabet is {0,1}. It needs to match the string {11,111,1111,11000...11..0011} However many 1's and 0's in between doesn't matter as long as it ends with 2 1's. So far I've got this:
^(1{2,4}|(11[01]*0[01]*11))$
But my answer wasn't accepted because it needs to be simplified. Something along these lines 11(0|1)*(11)* - this returns infinite 11's at the end so it's not accepted. I just can't figure it out can someone please push me in the right direction.
One possibility ^(?=11)[01]*11$. See demo. Here use look ahead to assert the string starts with 11 which fits the edge cases (11, 111) pretty well here since it doesn't consume characters, and then match the whole string with [01]*11$ which contains only 1 and 0 and ends with pattern 11.
Or based on your existing approach ^(1{2,3}|11[01]*11)$ should work as well. demo.
The simplest one:
11((0*1)*1)*
Explain:
When capturing 0 we must have one 1's at the end and another 1's at the outer group.
11 # match because 11 and Kleene star group is empty
111 # match 11(e1) -> 111
1111 # match 11(e1)1 --> 1111
11011 # match 11(01)1
11001 # non-match because 11(001) (no 1's at the end)
110111011 # match 11((01)1))(e1)((01)1)
^(1{2,4}|11[01]+11)$
^(1{2,3}|11[01]*11)$
^(11|111|11[01]*11)$
Your last answer is very close. (^11[01]*11$|^11+$) would do.
I added the OR 1+ to cover the 11 and 111 cases because the expression on the left covers anything that starts with 11 then either has some 0's and/or 1's or doesn't have them but then definitely has 11 again. This mean the shortest string it will match would be 1111. Hence the fix.
EDIT:
Sorry I answered too fast. Take Psidom's answer it's perfect.
Only 0 and 1?
And starts and ends with 11?
But also matching "11" or "111"?
Then this regex also does that:
^11(1|[01]*11)?$
Related
I hope you can help me, I'm studying how to make a regex and now I have this problem:
Write a regex that accepts strings with 0 and 1 and that has a 1 on position 5 from right to left.
e.g. 10000 is accepted because it has an 1 on the position 5 from right to left or 010000, 0010000 or 1110000 are accepted.
I was thinking with something like: (0+1)*+1(0+1)(0+1)(0+1)(0+1)(0+1)
You can use this regex:
1[01]{4}$
If you want to match full input then use:
^[01]*1[01]{4}$
Here 1[01]{4}$ ensures that we have 4 digits of 0 and 1 after we match 1 thus making 1 at 5th position from right to left.
RegEx Demo
Well - think of it this way. It needs to be as many 1s and 0s as you please, followed by a 1, followed by 4 more ones or zeroes.
So:
my_regex =
"^[01]*" + // Starts with One or zero, zero or more times
"1" + // Followed by a one
"[01]{4}$" // Followed by four things, which could be either zero or one, before ending.
Your (0 + 1) syntax looks foreign to me. I'm using character classes to specify the [01] things but you could use (0|1) in their place, which is what your attempt looks more like.
The full thing, together, is ^[01]*1[01]{4}$
I came across the regular expression not containing 101 as follows:
0∗1∗0∗+(1+00+000)∗+(0+1+0+)∗
I was unable to understand how the author come up with this regex. So I just thought of string which did not contain 101:
01000100
I seems that above string will not be matched by above regex. But I was unsure. So tried translating to equivalent pcre regex on regex101.com, but failed there too (as it can be seen my regex does not even matches string containing single 1.
Whats wrong with my translation? Is above regex indeed correct? If not what will be the correct regex?
Here is a bit shorter expression ^0*(1|00+)*0*$
https://www.regex101.com/r/gG3wP5/1
Explanation:
(1|00+)* we can mix zeroes and ones as long as zeroes occur in groups
^0*...0*$ we can have as many zeroes as we want in prefix/suffix
Direct translation of the original regexp would be like
^(0*1*0*|(1|00|000)*|(0+1+0+)*)$
Update
This seems like artificially complicated version of the above regexp:
(1|00|000)* is the same as (1|00+)*
it is almost the solution, but it does not match strings 0, 01.., and ..10
0*1*0* doesn't match strings with 101 inside, but matches 0 and some of 01.., and ..10
we still need to match those of 01.., and ..10 which have 0 & 1 mixed inside, e.g. 01001.. or ..10010
(0+1+0+)* matches some of the remaining cases but there are still some valid strings unmatched
e.g. 10010 is the shortest string that is not matched by all of the cases.
So, this solution is overly complicated and not complete.
read the explanation in the right side tab in regex101 it tells you what your regex does( I think you misunderstood what list operator does) , inside a list operator ( [ ) , the other characters such as ( won't be metacharacters anymore so the expression [(0*1*0*)[1(00)(000)] will be equivalent to [01()*[] which means it matches 0 or 1 or ( or ) or [
The correct translation of the regular expression 0∗1∗0∗+(1+00+000)∗+(0+1+0+)∗
will be as follows:
^((?:0*1*0*)|(?:1|00|000)*|(?:0+1+0+)*)$
regex101
Debuggex Demo
What your regex [(0*1*0*)[1(00)(000)]*(0+1+0+)*] does:
[(0*1*0*)[1(00)(000)]* -> matches any of characters 0,(,),*,[ zero or more times followed by
(0+1+0+)* --> matches the pattern 0+1+0+ 0 or more times followed by
] --> matches the character ]
so you expression is equivalent to
[([)01](0+1+0+)*] which is not a regular expression to match strings that do not contain 101
0* 1* ( (00+000)* 1*)* (ε+0)
i think this expression covers all cases because --
any number apart from 1 can be broken into constituent 2's and 3's i.e. any number n=2*i+3*j. So there can be any number of 0's between 2 consecutive 1's apart from one 0.Hence, 101 cannot be obtained.
ε+0 for expressions ending in one 0.
The RE for language not containing 101 as sub-string can also be written as (0*1*00)*.0*.1*.0*
This may me a smaller one then what you are using. Try to make use of this.
Regular Expression I got (0+10)1. (looks simple :P)
I just considered all cases to make this.
you consider two 1's we have to end up with continuous 1's
case 1: 11111111111111...
case 2: 0000000011111111111111...(once we take two 1's we cant accept 0's so one and only chance is to continue with 1's)
if you consider only one 1 which was followed by 0 So, no issue and after one 1 we can have any number of 0's.
case 3: 00000000 10100100010000100000100000 1111111111
=>(0*+10*)1
final answer (0+10)1.
Thanks for your patience.
I have a string of 8 separated hexadecimal numbers, such as:
3E%12%3%1F%3E%6%1%19
And I need to check if the number 12 is located within the first 4 set of numbers.
I'm guessing this shouldn't be all that complex, but my searches turned up empty. Regular expressions are always a trouble for me, but I don't have access to anything else in this scenario. Any help would be appreciated.
^([^%]+%){0,3}12%
See it in action
The idea is:
^ - from the start
[^%]+% - match multiple non % characters, followed by a % character
{0,3} - between 0 and 3 of those
12% - 12% after that
Here you go
^([^%]*%){4}(?<=.*12.*)
This will match both the following if that is what is intended
1%312%..
1%123%..
Check the solution if %123% is matched or not
If the number 12 should stand on its own then use
^([^%]*%){4}(?<=.*\b12\b.*)
Am playing around with regexp's but this is my headache. I have a dynamic number which needs a suffix. The suffix is always 0 to 9, 99 or 999.
Example:
I have the number 461200 and now I want to create an regexp that will match 461200 to 461209. What I've learned it should be ^46120[0-9]$? Is this correct or somewhere to the left of hell?
Ok, let us assume it is correct and I now want to match 461200 - 461299? This is where I get lost.
^4612[0-9]{2}?
It cannot be. I am yet to figure this out.
Any help appreciated.
For 1 digit at the end you need:
^4612[0-9]$
2 digits at the end:
^4612[0-9]{2}$
3 digits at the end:
^4612[0-9]{3}$
The number in braces {} means the number of time the preceding character or set has to be repeated.
Ok, let us assume it is correct and I now want to match 461200 -
461299?
You can either repeat the desired character class by saying [0-9][0-9] or use quantifiers [0-9]{2}.
It can be either:
^4612[0-9][0-9]$
or
^4612[0-9]{2}$
Both would work.
maybe try this regex:
^4612\d{2}$
I want a regex that checks the following things:
The string starts with an +
After the '+' only numbers can occur
There should be atleast 4 numbers after the +
Does anyone know how to make this?
/^+\d{4,}$/
will meet your requirements.
^ is the anchor for start fo the string
\d is a digit
{4,} says at least 4 of the preceding expression (here the \d). you can add a maximum if needed like {4,20} would allow at least 4 and at most 20 characters.
$ is the anchor for the end of the string
/^((00|\+)[0-9]{2,3}){0,1}[0-9]{4,14}$/
More general than your request, but you can specialize it. Explaining:
((00|\+)[0-9]{2,3})
international code with 00 or + and 2 or 3 digits. Modify the expression according to your needs.
{0,1}
international code is optional - remove it if it is required
[0-9]{4,14}
digits: minimum 4, maximum 14. Change the values according to your needs.
Regards
A.
/\+\d{4,15}/
This should help if 15 is the atmost limit of numbers
OR rather keep the second parameter blank as stema suggested.
I went with this one:
/\A(([+]\d{3,})?\d{6,8})/