tags:

views:

36

answers:

3

Im handling various email addresses that come in the following forms

John Doe <[email protected]>
[email protected]

How could i use regex, to find the @ symbol, and then return the integer's behind it (until it doesnt find anymore, or runs into a non number, < or space for example.)

+1  A: 

Just search for this regex:

(\d*)@

And then look at the first capture group.

Laurence Gonsalves
+2  A: 
/^\D*(\d*)@/

Will match any number of non-digits, any number of digits, followed by an @.

The capturing group will contain the digits.

Anon.
could you provide an example of this usage in php?
Patrick
Chris Gutierrez's answer has an example of the syntax for regex matching in PHP.
Anon.
A: 

You could try something like...

preg_match("#^[\D]+\<([\d]+)#", "John Doe <[email protected]>", $matches);
Chris Gutierrez
This won't match the second example posted.
Anon.