0
"Max Mustermann" <max.mustermann@domain.com>
max.mustermann@domain.com
Max <max.mustermann@domain.com>

I need a regular Expression which matches everthing outside the arrow brackets (including the brackets). The Match should be removed afterwards.

After the replacement it should look like this:

"Max Mustermann" <max.mustermann@domain.com> => max.mustermann@domain.com
  • Although not the same question, note that fully parsing all forms of email addresses is hard, http://stackoverflow.com/questions/46155/validate-email-address-in-javascript. – hlovdal Feb 14 '13 at 11:46

2 Answers2

0

The easiest solution would be to search for

[^<]*<([^>]*)>.*

and replace that with \1 or $1, depending on your regex engine.

This removes everything until the first < and everything from the next > until the end of the string.

Let's just hope that there will be no brackets inside the quoted names.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • it does not really. For example, naked `email@domain.com` does not match. – mvp Feb 14 '13 at 11:45
  • because it should extract email address from any email, including without `<>` - this is listed as line 2 in question. – mvp Feb 14 '13 at 17:47
  • @mvp: OK, so what exactly should be matched and removed from `max.mustermann@domain.com`? – Tim Pietzcker Feb 14 '13 at 18:00
  • this is in my answer - http://fiddle.re/ub81 (click Perl or Java to see examples) – mvp Feb 14 '13 at 18:04
  • Point is that on input we have email address that could be in all kinds of weird formats (with descriptive user name, with angle brackets or without), and on output we want email in simplest possible format `user@domain.com`. – mvp Feb 15 '13 at 01:54
  • @mvp: Where does it say that in the question? – Tim Pietzcker Feb 15 '13 at 06:17
0

This should work, but beware that it is very simplified:

(?:[^<]*<)?([^>]+).*

Answer of email will be in $1.

For example, in Perl use:

$email =~ s/(?:[^<]*<)?([^>]+).*/$1/;

See RegexPlanet online demo.

Community
  • 1
  • 1
mvp
  • 111,019
  • 13
  • 122
  • 148