Search code examples
phpregexpreg-match

Regex/preg_match - get text between string and email address


I'm extracting data from emails. I have pieces of text like this:

Eg. 1: some standard text.   Bugs Bunny [email protected] 0411111111 more standard text 
Eg. 2: some standard text.   Bugs The Bunny [email protected] 0411111111 more standard text
Eg. 3: some standard text.   Bugs Bunny [email protected] 0411111111 more standard text
Eg. 4: some standard text.   Bugs [email protected] +6141 111 111 more standard text

As you can see, there is a name, email and phone number that I want to extract. The email should be easy enough, and I'm sure I can work out the phone options but how could I get the name?

I know the logic is: get the text after some standard text. and before the the first non-space-separated string before the @, but how?

This is my starting point (?<=some standard text. )(.*?)(?=@)

This gives me a result with a group (?<=some standard text. )(.*?)(?:[\w-\.]+)@ so I think I'm on the right path.

I'm using php.


Solution

  • Here is a quick version/example I came up with:

    (?<=some standard text. )(.*?) ([^\s]+@[^\s]+) (\+?\d+(?:\s\d+)*) 
    

    regex101.com/r/Wjz66g/1

    It's not perfect, but it does follow along the same lines as what you were doing and might work enough.