In this post
Regular expression negative lookbehind of non-fixed length
the answerer says, to match things only to ignore them. I want to use that example, but I want to print only the matches that are not ignored.
In this post
Regular expression negative lookbehind of non-fixed length
the answerer says, to match things only to ignore them. I want to use that example, but I want to print only the matches that are not ignored.
If you want to make sure that, foo
should be followed by bar
and if you are only interested in bar
, then you can use look-behind assertion, like this
re.findall("(?<=foo )bar", "foo bar")
# ['bar']
Instead of bar
, if you want to match anything followed by foo
, you can do
re.findall("(?<=foo ).*", "foo google")
# ['google']
This will leave empty items in the list. But for what I assume you are asking, you can use the alternation operator in context placing what you want to exclude on the left, ( saying throw this away, it's garbage ) and place what you want to match in a capturing group on the right side to only print the captured matches.
>>> re.findall(r'foo|(bar)', 'foo bar foo bar')
['', 'bar', '', 'bar']