Hello, I have a string of the form "required optional3 optional2 optional1 optional3" ('optional' may be any kind of string, so it's not simply 'optional\d+'. I would like to use a regex so I can distinguish groups. Desired outcome: ('required', 'optional3', 'optional2', 'optional1', 'optional3'). Below is a fragment of the many things I have tried. >>> import re >>> regex = r"(required) (optional1)* (optional2)* (optional3)*" >>> #regex = r"(required) (?:(optional1)*|(optional2)*|(optional3)*)*" >>> #regex = r"(required) (optional1|optional2|optional3)*" >>> s = "required optional3 optional2 optional1 optional3" >>> re.search(regex, s).groups() Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> re.search(regex, s).groups() AttributeError: 'NoneType' object has no attribute 'groups' >>> s2 = "required optional1 optional2 optional3" >>> re.search(regex, s2).groups() ('required', 'optional1', 'optional2', 'optional3') # it only 'works' if the optional words are in the same order as in the regex, and not specified multiple times.
How can I make this work? Thank you in advance! Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor