Search code examples
pythonregexregex-lookarounds

How to get all the words "bar" after the word "foo" with regex


I am trying to retrieve all occurrences of the word bar after the word foo.

Below the content:

fuu

bar

faa

foobar fuu

bar fuubar

bar

bar fuu

I want to retrieve all bars that are bold and disregard the first bar that is in italic format.

I tried to use the follow regular expression:

(?<=foo)bar

But that only catches the first occurrence.

UPDATE

Thanks for the support guys. Below the data more closer to reality:

Some data

name: Person 1

Some data

my_delimiter:

 name: Person 2

 Some data

name: Person 3

Some data

 name: Person 4

 Some data

Some data

I want to get the name of the persons after my_delimiter:

I am testing here https://regex101.com/r/HrCLva/2


Solution

  • After you updated your answer, you don't need a regex lookbehind, you can use a regex like this to find the first name after your delimiter:

    my_delimiter:\s+name:\s*(.*)
    

    Working demo

    On the other hand, if you want all names after your delimiter, then you can use a regex trick like this and then grab the content from capturing group:

    [\s\S]*my_delimiter|name:\s*(.*)$
    

    Working demo 2

    The capturing group stores the data highlighted in green.

    enter image description here