Reputation: 101
I'm trying to match the input string by both given conditions. For example, if I give '000011001000'
as input and want to match it by '1001'
and '0110'
then what would the regex I need look like?
I tried different combinations, but couldn't find the correct one. The closest I got was using
re.match("(1001.*0110)+?")
but that one doesn't work when input is for example '0001100100'
.
Upvotes: 1
Views: 266
Reputation: 5515
This pattern makes use of "look-arounds" which you should learn about for regex.
(?=[01]*1001[01]*)(?=[01]*0110[01]*)[01]+
in response to the comments:
look-arounds in regex are a simple way of checking the match for specific conditions. what it essentially does is stop the current match cursor when it hits the (?=
(there are also others suchs as ?!, ?<=, and ?<!
) token and reads the next characters using the pattern inside of the lookaround statement. if that statement is not fulfilled then the match fails. if it does, then the original cursor then keeps matching. imagine it being a probe that goes ahead of an explorer to check the environment ahead.
if you want more reference, rexegg is probably my favourite site for learning regex syntax and nifty tricks.
Upvotes: 2