0

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.

7
  • It would help if you said what went wrong. (What did you expect to happen? What actually happened?) Commented Feb 24, 2013 at 2:03
  • Why not just ask for their full name and email address as two seperate inputs...? Commented Feb 24, 2013 at 2:03
  • @vcsjones Developing is not that simple job to do. Isn't it? ^^; Commented Feb 24, 2013 at 2:05
  • @KendallFrey As I mentioned in my question, I expect to abstract people's name and email address like "Peter Jackson" and "<[email protected]>". Even if they write "<", "(", "[" or "[". But with my expression, it returns blank. Commented Feb 24, 2013 at 2:06
  • Also, for regex questions, it's very helpful to have either A. a list of strings that should match the regex, and a list of strings that should not, or B. A string or list of strings, and a list of the substrings that should match. Commented Feb 24, 2013 at 2:06

3 Answers 3

1

I believe the regular expression you are looking for is as follows:

(.*?)[<([{](.*?)[>)\]}]

You would want group 1 and group 2.

Sign up to request clarification or add additional context in comments.

Comments

0
(.*?)[([<{](.*?)[)\]>}]

I believe this should be adequate.

The name will be in the first captured group, and the email will be in the second.

1 Comment

Thanks to you, too. It also works. I didn't know about grouping with [].
0

Seems like (.*)[<\(\[{](.*)[>\)]}] works for me.

Groups for the name, and the email address as well.

http://regexr.com?33sq4 is my test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.