2

Does any one have an idea how to match something like

<t@t.com>

I tried this regular expression

\<(.?*)\>

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user3150060
  • 1,725
  • 7
  • 26
  • 46
  • 1
    Just read [this](https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression) and add <> around it – apokryfos Jul 22 '21 at 21:25
  • The art of programming is breaking problems down - don't expect to find someone who's written the exact code you need, but do look for sub-problems that might be common. – IMSoP Jul 22 '21 at 21:41

2 Answers2

2

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 = "<t@t.com>\n<tatacom>\n<t@ata@com>";
$re = '/<([^<>@]+@[^<>@]+)>/';
if (preg_match_all($re, $str, $matches)) {
    print_r($matches[1]);
}
// => Array( [0] => t@t.com )
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You can use this:

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

MendelG
  • 14,885
  • 4
  • 25
  • 52
  • I'm not sure what the `[,-]` sections are trying to do. `-` is not special in an email address. And you're assuming all top-level domains have 2 or 3 letters, which is not true (.info, .news, etc). Plus, `\w` is far too restrictive; email addresses allow more than that. `*` is valid, for example. In general, matching email address is FAR more difficult than most people think. – Tim Roberts Jul 22 '21 at 21:14