user3150060
user3150060

Reputation: 1745

Regular Expression to extract strings between angle brackets with exactly one @ char in between

Does any one have an idea how to match something like

<[email protected]>

I tried this regular expression

\<(.?*)\>

but this matches also <sddsds> I want it to match where inside <> is an email with @ sign.

Upvotes: 2

Views: 108

Answers (2)

You can use this:

$regex = '/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/'

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You can use

<([^<>@]+@[^<>@]+)>

See the regex demo. Details:

  • < - a < char
  • ([^<>@]+@[^<>@]+) - Group 1: any one or more chars other than <, > and @, a @ char and then again any one or more chars other than <, > and @
  • > - a > char.

See the PHP demo:

$str = "<[email protected]>\n<tatacom>\n<t@ata@com>";
$re = '/<([^<>@]+@[^<>@]+)>/';
if (preg_match_all($re, $str, $matches)) {
    print_r($matches[1]);
}
// => Array( [0] => [email protected] )

Upvotes: 2

Related Questions