I expected it not match. I can see how 11 matches: you just take the entire RE once but with no zeroes inside, so you need 11 because that's not optional. Now, how can we get 00 at all from this RE?
Asked
Active
Viewed 76 times
-5
-
1There are some online graphical views of regex. Here's one: [(0*1(00)*10*)*](https://www.debuggex.com/r/zUbawcM2mZfkTo6Q). As long as it matches "optionally", it matches. The regex matches many strings. – lurker May 23 '19 at 22:39
-
I don't understand that drawing. How do I read it? How does it help? – user9654148 May 23 '19 at 22:42
-
1The drawing corresponds to your regex. `0*` is illustrated by the loop around the first `0` in the diagram, etc. You should try to walk through it step by step, as it will help you understand regex. In your case, you get the `00` at the end simply because 0* matches zero *or more* `0` characters. Thus the regex not only matches 11, but also 110, 1100, 11000, etc. – lurker May 23 '19 at 22:44
-
You did spot my problem. Indeed: I can use 0 of 0*, 1 of 1, 0 of (00)*, 1 of 1 then 2 of 0* getting 1100. I finally see it. Thank you! – user9654148 May 23 '19 at 22:47
-
1Glad it's clearer now! You should play with that website with the diagram. Start with a very simple regex (like `0*` or `0?`) and see what the diagram shows. Then try something a little more complicated (maybe `[abc]+` etc. If you're trying to understand how regex works, start simple. – lurker May 23 '19 at 22:48
-
Special thanks for the educational instructions! – user9654148 May 23 '19 at 22:50
-
https://regex101.com/r/YWgTsz/1 – Rubens Farias May 23 '19 at 22:51
-
Your regex can equate to `1100` that's why it matches. – May 23 '19 at 23:23
2 Answers
2
You match the first 1. Then (00)* doesn't match anything so you still have a 1. Then you match 1 followed by zero or more 0's. So you have 11. then 0* or matches 00. So 1100.
- 0*1 matches the first 1.
- (00)*1 matches the second 1.
- Then 0* matches 00

WJS
- 36,363
- 4
- 24
- 39
-
-
-
I see your answer now. Your bullets look correct, but your initial paragraph does not. Notice how you say "zero or more of 00" twice before matching the second 1. It doesn't look correct to me. – user9654148 May 23 '19 at 22:50
0
( : first capturing group
0 * : character 0 zero or more times
1 : char '1'
( : second capturing group
00 : characters "00"
)* : group matches zero or more times
1 : char '1'
0 *: char '0' matches zero or +
)* All capturing group can matches zero or more times
So 1100 matches

nissim abehcera
- 821
- 6
- 7