-3

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.

Community
  • 1
  • 1
Mark Galeck
  • 6,155
  • 1
  • 28
  • 55

2 Answers2

2

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']
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

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']
hwnd
  • 69,796
  • 4
  • 95
  • 132