Jump to content

[SOLVED] very simple eregi issue


rckehoe

Recommended Posts

I have a very simple issue that I cannot figure out, I really suck at regular expressions...

 

I have a bit of text formatted like so: First Last Name <[email protected]>

 

I want to extract the Email Address from the < and >, how is this done? I am using this code, but obviously it is wrong!

 

$SearchEmail = eregi("[<(.)>]", $r, $regs);

Link to comment
https://forums.phpfreaks.com/topic/155522-solved-very-simple-eregi-issue/
Share on other sites

[<(.+)>]

 

Post #11 and 14 from this thread provide a caveat to using .+ (or even .*, same principal). If <[email protected]> is the complete string in itself, then it's not a big deal at all to use greedy quantifiers..(in fact, would be even preferable to do so, as backtracking would be minimal) but if this is all nested in a larger body of text, it would be wise to avoid such greedy quantifiers and use lazy quantifiers instead.

 

$string = "First Last Name <[email protected]>";

preg_match("~<(.*?)>~i", $string, $match);

 

Just to note that in this case, the i modifier isn't necessary, as the dot match all will match anything (including upper / lowercase alpha characters) other than a newline (that is, in the absence of the s modifier).

 

Another alternative to lazy quantifiers include the use of a negated character class such as:

~<([^>]+)>~

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.