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.
Related
Here's a CFG that generates strings of 0s, 1s, or 0s and 1s arranged like this (001, 011) where one of the characters must have a bigger count than the other like in 00011111 or 00000111 for example.
S → 0S1 | 0A | 0 | 1B | 1
A → 0A | 0
B → 1B | 1
I tried converting it to regular expression using this guide but I got stuck here since I have trouble converting 0S1 given that anything similar to it can't be found in that guide.
S → 0S1 | 0+ | 0 | 1+ | 1
A → 0A | 0 = 0+
B → 1B | 1 = 1+
One of my previous attempts is 0+0+1|0+1+1|1+|0+ but it doesn't accept strings I mentioned above like 00011111 and 00000111.
Plug and Play
^(?!01$)(?!0011$)(?!000111$)(?!00001111$)(?=[01]{1,8}$)0*1*$
You cannot perfectly translate this to a regular expression, but you can get close, by ensuring that the input does not have equal number of 0 and 1. This matches up to 8 digits.
How it works
^ first you start from the beginning of a line
(?!01$) ensure that the characters are not 01
(?!0011$) ensure that the characters are not 0011
the same for 000111 and 00001111
then ensure that there are from 1 to 8 zeroes and ones (this is needed, to ensure that the input is not made of more digits like 000000111111, because their symmetry is not verified)
then match these zeroes and ones till the end of the line
for longer inputs you need to add more text, for up to 10 digits it is this: ^(?!01$)(?!0011$)(?!000111$)(?!00001111$)(?!0000011111$)(?=[01]{1,10}$)0*1*$ (you jump by 2 by adding one more symmetry validation)
it is not possible by other means with regular expressions alone, see the explanation.
Explanation
The A and B are easy, as you saw 0+ and 1+. The concatenations in S after the first also are easy: 00+, 0, 11+, 1, that all mixed into one lead to (0+|1+). The problem is with the first concatenation 0S1.
So the problem can be shorten to S = 0S1. This grammar is recursive. But neither left linear nor right linear. To recognize an input for this grammar you will need to "remember" how many 0 you found, to be able to match the same amount of 1, but the finite-state machines that are created from the regular grammars (often and from regular expressions) do not have a computation history. They are only states and transitions, and the machinery "jumps" from one state to the other and does not remember the "path" traveled over the transitions.
For this reason you need more powerful machinery (like the push-down automaton) that can be constructed from a context-free grammar (as yours).
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 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.
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.