javaregex

Managing Regex in java


How we can manage regex in Java?

Yes, I did search regex topics, but I think it is strange in Java. What I would like to do is

My team <xxx-yyyy@uuuu.com> 

with regex, I would like to get string between < > as xxx-yyyy@uuuu.com

Pattern p = Pattern.compile("(.+?)\\<.*?\\>?");

The above one didn't work.


Solution

  • Something like this, maybe?

        final Pattern p = Pattern.compile("(.+?)<(.*?)>");
        final Matcher matcher = p.matcher("Foo bar <xxx-yyy@aaa.bbb>");
        if (matcher.matches()) {
             System.out.println(matcher.group(2));
        }
    

    Edit: please also see @WhiteFang34's answer below which is a much better approach if you are dealing with email addresses.