I’m learning regex, and I can’t understand the grouping in Java.
Currently my regex expression is
([^:]?)(?![email])(\w+[\. ]?\w+)\ ?\@[\. ]?(\w+\.?\w+)\.edu
My goal is the extract email addresses that come in various formats. An example of a line of string that doesn’t work with this is:
To get on the vcode mailing list, please email engler@lcs.mit.edu.
My expression matches the correct string but
group(1) = e
group(2) = ngler
What I want is
group(2) = engler
It works for other strings such as href=”mailto:balaji@stanford.edu”>
Thanks for taking the time to help me out.
By the way I’m using http://www.regexplanet.com/advanced/java/index.html to help me out, it converts regex expressions into ones that java understands and shows you the groupings.
Thank you Affe
Answer:([^:]?)\b(?!(email))\b(\w+[. ]?\w+)\ ?\@[. ]?(\w+.?\w+).edu , I didn’t know it was treated by characters, thank you.
The expression [email] is a character class. it matches any one of the letters e, m, a, i, or l. It does not match the word “email.” That would just be email without the brackets. That’s why it picks up the e in engler.