python - Match but not return regular expression between two matches -


i'm trying retrieve 2 distinct items without matching term in between using regular expressions:

[[a]] son of [[b]]

i thought work:

\[\[[a-za-z\s]+\]\](?:(.*son of.*))\[\[[\|,a-za-z\s]+\]\]

but it's not quite working. how can match , b without returning "son of"?

i assume have string [[worf]] son of [[mogh]], want "return" (i.e. match) [[worf]] , [[mogh]]. so, you'll need capturing groups (parentheses) around portions of regular expression want capture (aka "return"). in order not return son of, you'll need remove capturing group have in there (nested within (?:...)).

in code:

>>> s = '[[worf]] son of [[mogh]]' >>> p = re.compile('(\[\[[a-za-z\s]+\]\])(?:.*son of.*)(\[\[[\|,a-za-z\s]+\]\])') >>> print p.match(s).groups() ('[[worf]]', '[[mogh]]') 

if you're using .groups() method matched sections, can drop non-capturing group around .*son of.*, may make re more readable (i happen think makes intention -- not capture -- more clear).


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -