java matcher doesn't process ^ and $ symbols
There is email pattern:
^[a-zA-Z0-9][\w.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z.]*[a-zA-Z]$
and the example code, that try to get email from string:
String email = "NAME <firstname_lastname@domain.com>";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
if (matcher.find()) {
System.out.println(matcher.group());
}
When regexp contains ^ and $ characters, matcher.find() returns false, but
when regexp is without these characters, all is fine: I see
firstname_lastname@domain.com in console.
Could you please explain difference between regexp with and without ^ and $?
How can I force the Matcher to work with regexp that contains ^ and $?
No comments:
Post a Comment