Search code examples
phpregexpreg-match

Regex - get group 1 as "full match"/group 0


Given the following examples, I want to get the email address

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
Eg. 5: some standard text.   Bugs o'Bunny [email protected] 0411111111 more standard text 

This will do it: (?<=some standard text. )(?:.*?)([^\s]+@[^\s]+) https://regex101.com/r/A29hjE/9

But the email address is in group 1. I need it to be group 0 or the full match because this regex will be created dynamically by some code in which all other regex's produce their findings as the full match.

I don't know enough about groups, but I know I need the first email address after the some standard text. bit and, like I said, it need's to be the full match.


Solution

  • If you change your regex to simply be ([^\s]+@[^\s]+), the full result should be just the email address.