Reputation: 1897
Good day~ I am not really good at this regular expression. So I need your help, please.
Condition: Users can input their email addressed and name together. I want to extract email address and user name out of string.
string pattern1 = "Peter Jackson<[email protected]>";
From that string I want to get "Peter Jackson" and "<[email protected]>".
string pattern2 = "Peter Jackson([email protected])";
However, people always make mistakes like below.
And they can also use "[" instead of "<". so...
string pattern3 = "Peter Jackson[[email protected]]";
Even some stupid users can input like...
string pattern4 = "Peter Jackson{[email protected]}";
So, I had to look for the characters which are "<", "(", "[" and "{". I tried
string regularExpressionPattern = @"^(<|(|[|{)(.*?)^(}|]|)|>)";
But I think I've done something wrong. And I also try to think that people could input more mistake like....
string pattern5 = "Peter Jackson<[email protected]>mistake";
Could anyone help this problem? Advanced thanks.
PS: I know how to split string with a character. So it won't help. I needa proper regular expression.
Upvotes: 0
Views: 393
Reputation: 16585
Seems like (.*)[<\(\[{](.*)[>\)]}]
works for me.
Groups for the name, and the email address as well.
http://regexr.com?33sq4 is my test.
Upvotes: 0
Reputation: 44316
(.*?)[([<{](.*?)[)\]>}]
I believe this should be adequate.
The name will be in the first captured group, and the email will be in the second.
Upvotes: 0
Reputation: 11591
I believe the regular expression you are looking for is as follows:
(.*?)[<([{](.*?)[>)\]}]
You would want group 1 and group 2.
Upvotes: 1