I have a simple question about finding a regular expression for a given language.
I am given the language L where:
L = {w ∈ {0, 1}* : w has exactly one pair of consecutive zeros}
My first attempt at this was to try L( (0 + 1)* 00 (0 + 1)*), but I noticed the problem with that would be with where I have (0 + 1)* because if 0 is chosen, it can be zero more more of them, thus leading to more than one pair of consecutive zeros.
I also know, that the possible cases I have are, two zeros in the front, in the middle, and at the end. I just am not quite sure how to create a regular expression for that.
Any help is much appreciated.
Try this:
1* (011*)* 00 (11*0)* 1*
An explanation:
1*: any amount of leading 1’s
(011*)*: if there is a 0 before the 00, it must not be followed by another 0, thus only one or more 1’s are allowed; this pattern may be repeated any number of times
00: the two 0’s
(11*0)*: if there is a 0 after the 00, it must not preceded by another 0, thus only one or more 1’s; this pattern may be repeated any number of times
1*: any amount of trailing 1’s
The best possible answer for this problem is (1 + 01)* 00 (1 + 10)*
i believe it would be like this
((1*)(01)*))* 00 ((11*)0)*1*
The sequence:
Anything but 00, ending with 1
00
Anything but 00, starting with 1
My answer would be : (1 + 01) 00 (1 + 10)**
Explanation:
The consecutive zeros shouldn't be preceded or followed by another zero.
Hence 00 should be preceded by a 1 which can be either a 1 or 01.
It can be followed by a 1 or 10.
Related
I have a regex ".*00$" which restricts that the input must be ending with 00. How can I improve this to add a max length of 20 also. So:
100 => valid,
00 = > invalid,
12345678900 => valid,
111111111111111111100 = > 21 digits - invalid.
Based on your title and short description this should probably work for you:
^[0-9]{1,18}00$
This will allow 3-20 digits in input that ends with 00
This can be expressed almost literally as
/^[0-9]{1,18}00$/
20 digits, the last two being zeros.
You can use {} to specify a range of characters, so
".{1,18}00"
would allow any 1-18 characters followed by 00. If you want to restrict it further, you could use
"[1-9][0-9]{0,17}00"
so that you ensure the first number is not 0, followed by 0-17 numbers and finally 00.
Try this :
^[1-9][0-9]{0,17}00$
The first [] will ensure it doesn't start by 0.
I've puzzled a lot, but can't figure out why my regex doesn't work.
It's for an input which should accept a numerical range between 0 and 40. It should reject 0 and 00, but accept 1 or 01 and further...
Where am I wrong?
\b([1-3]?\d{1}|40)\b
[1-3]?\d{1}
matches 0 because [1-3] is optional and \d of course includes 0. Also, the {1} is a no-op - every token is matched once by definition.
You need something like this:
\b(0?[1-9]|[1-3]\d|40)\b
I'm trying to create a regex to accept digits not starting with zero or a single zero digit.
Example matches
0
50
798
Example rejects
01
046
0014
00
0001
My attempt was to use /[0]|[1-9][0-9]*/ to match the values in the following text:
0, 50, 798
01, 046, 0014, 00, 0001
This attempt can be run at http://regexr.com/3bb00
Use following regex :
^(0|[1-9]\d*)$
see Demo https://regex101.com/r/zT8uI2/2
This regex contains 2 part, 0 or [1-9]\d* which is a digit that doesn't starts with zero.
Note that if you want to match your numbers within other texts you need a word boundary instead of start and end anchors :
\b(0|[1-9]\d*)\b
see demo https://regex101.com/r/zT8uI2/3
It seems that you have two cases in your regex:
Match a single zero
Match digits that don't start with zero.
The first case is easy: /0/
The second case is also pretty easy /[1-9]\d*/. The [1-9] matches the digit that is not 0. Then, we can have 0 or more digits.
To get both of these cases, just use a bar to do either or
/0|[1-9]\d*/
Hmm, why not something like..
if(input[0] == '0' && input.size() > 1) // reject
else //accept
Please check this http://regexr.com/3bb09
Took the tip from https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch06s06.html
and improved it to negate numbers starting with 0.
RE: \b[^0,]*([1-9][0-9]*|0)\b
Text: 0, 50, 798, 01, 046, 0014, 00, 0001
Matched only 0, 50 and 798
Thanks
Venkat
I am studying course for Automata, may be as a beginner I'm missing something , I am really confused with this regular expression as the teacher saying
it represents a language with all binary strings having even number of
symbols
Alphabet Σ={0,1}
((0+1)(0+1))*
(0+1) saying union of 0 and 1
but I am confused here let suppose
first (0+1) gives 01 and second (0+1) gives only 0, then finally we concatenate both having (010)* and let it end up with only one occurrence i.e. 010
then how it has even number of symbols? need little understanding of this...
The given Question: ((0+1)(0+1))*
Let's start from the innermost bracket (0+1) this matches with either 0 or 1 not 01
'+' means "or" operator, this is not a conjunction operator.
and this is repeated twice hence ((0+1)(0+1)) matches with 01 or 10 or 00 or 11
and '*' means it matches 0 or more times
Hence, ((0+1)(0+1))* matches with [NULL] or 01 or 10 or 00 or 11 or 0011 or 1100 or 1010 or 0110 ....so on
Union means one or the other, not both. So, 0+1 would either be 0 or 1, but not both at the same time. Hence (0+1)(0+1) would result in either 00, 01, 10 or 11. All of them have even number of symbols. The result would follow.
I'm just learning regular expressions, so I just want to make sure my understanding is correct.
01* mean 0 followed by 0 or more repetitions of 1.
1* + 01* means either 0 or more repetitions of 1 OR 0 followed by 0 or more repetitions of 1.
Am I right or is there something that I'm missing? Thanks.
The + in a regex doesn't mean OR but "one or more of"
So instead of 1* + 01* you would say:
1*|01*
which would mean either a (maybe zero length) string of ones, or a zero followed by (maybe a zero length) string of ones.
So it would match any of:
1
1111
0
011
But none of:
101
110
100001
001
00
The OR operator (vertical pipe) has a low precedence.
This seems correct to me. (Even thought I'm not a whiz in regular expressiony myself)
But here's a good tutorial you could check out.
This one I found useful as well.