Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Capture list of targets after cue

I am trying to create an expression that will retrieve a target or list of targets after a cue. I can currently get the first one, but I seem to be doing something wrong to get the repetitions. This is what I have so far:

import regex

text = "Some text cue: target, target, target and target. Other text."

expression = regex.compile(
fr"""
(?:cue)  # cue before targets. non capture (?:)
(?:.*?)  # text before the match. non capture (?:), as short as possible (?)
(target)
""",
re.VERBOSE,
)

matches = regex.findall(
    expression,
    text,
    overlapped=True,
)

I have tried (target,\s)* but that is not working.

(Note that the real-use case will be more involved, where targets are actually a collection of strings, etc.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The ideal output should be:

["target", "target", "target", "target"]

>Solution :

You can use

import regex

text = "Some text cue: target, target, target and target. Other text."

expression = regex.compile(fr"""
cue:  # cue before targets
(?:\s*(?:(?:,|\band\b)\s*)?(?P<targets>target))+
""",
regex.VERBOSE,
)

match = regex.search(expression, text)
if match:
    print( match.captures("targets") )

# => ['target', 'target', 'target', 'target']

See the Python demo.

The cue:(?:\s*(?:(?:,|\band\b)\s*)?(?P<targets>target))+ regex matches

  • cue: – a string
  • (?:\s*(?:(?:,|\band\b)\s*)?(?P<targets>target))+ – one or more sequences of
    • \s* – zero or more whitespaces
    • (?:(?:,|\band\b)\s*)? – an optional sequence of a comma or a whole word and followed with zero or more whitespaces
    • (?P<targets>target) – Group "targets" matching target
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading