Anna Jeanine Anna Jeanine - 1 year ago 72
Python Question

Python: Regular Expression pattern can occur multiple times but is NOT conditional

Hi everyone! I am having trouble formatting a regex in

. What I would like the regex to find is
, but after this a
can follow. But this pattern of
can occur multiple times (or, not at all). The regex which I am using now is:'_D(D|2)*', variable)

I have tried:'_D(D|2)(D|2)*', variable)

But this didn't work for me either. Could someone help me out to make the pattern optional but multiple times?

Answer Source

The * suffix means "0 or more times" and therefore already implies optionality. You want to put it after (D|2) for that group to be repeatable:'_D(D|2)*', variable)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download