3
No, the string doesn’t necessarily have to start with 1
.
After all, 1*
that is to say "zero or more occurrences of 1
". The quantifier *
admits zero occurrences, which means that the 1
is not mandatory (see some examples).
Just one detail: regex has a quantifier around the parentheses: (...)*
, which means that everything between parentheses can also occur zero times. That is, regex also accepts empty strings (or "nothing" - see here how she finds a match).
Although zero is an even number, then "zero occurrences" of digit 0
is also an "even number of zeros" (but if the string is not empty, there must be at least two zeroes).
If you do not want to consider an empty string valid, you can change the regex to (1*01*01*)+
, for the quantifier +
means "one or more occurrences", meaning that what is in parentheses must occur at least once. Thus, the empty string is no longer considered valid.
Thank you very much for the reply, it was clear. But now I noticed something else that left me in doubt. In the case the string containing only a couple of zeros
00
would be accepted? Or obligatory it will have0101
– Pirategull
@Pirategull A pair of zeros accepts yes: https://regex101.com/r/lwuDlv/4/ - please note that all
1
has a*
soon after, then all may occur zero or more times.– hkotsubo
Gosh, how nice the link you sent, I didn’t know will be very helpful thank you. But I still can’t quite understand how these expressions work. (Edit: now I get it, I was considering it as if it were
(01)*
. Sorry– Pirategull
@Pirategull Regular Expressions is a very complex subject. I suggest starting with here and then here
– hkotsubo
Perfect! Thanks for the suggestions. If I may ask one last question. On the regex101 website, how do I represent sigma? (which can assume both
0
as1
)– Pirategull
@Pirategull If you want sigma character itself (
Σ
), just put it on. If you mean you can have one0
or1
, can use[01]
: https://regex101.com/r/WuqtcJ/1/– hkotsubo
eternally grateful
– Pirategull