-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
  • 5,671
  • 1
  • 23
  • 48

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
  • 221,210
  • 51
  • 432
  • 478
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
  • 67,942
  • 4
  • 86
  • 123