Jump to content

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@TheirEmail.com>

 

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@TheirEmail.com> 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@TheirEmail.com>";

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:

~<([^>]+)>~

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.