I have a requirement where in a request containing field es in to my rest webservice.
In my webservice, I have to check for this field and if the validation for this passes, then I send the request to a third party service.
Validation Required:
message_from
field contains an email address
as string
. I have to check if the domain name
(everything after @
) is roin
For ex: [email protected]
passes, [email protected]
passes, [email protected]
fails...
Can I use pattern matchers or anything else to do this validation?
I have used string parsing to capture everything after (@)
and then did an equalsIgnoreCase
to pare it with roin
This string parsing approach works, but is there any better way to do this?
I have a requirement where in a request containing field es in to my rest webservice.
In my webservice, I have to check for this field and if the validation for this passes, then I send the request to a third party service.
Validation Required:
message_from
field contains an email address
as string
. I have to check if the domain name
(everything after @
) is roin.
For ex: [email protected]
passes, [email protected]
passes, [email protected]
fails...
Can I use pattern matchers or anything else to do this validation?
I have used string parsing to capture everything after (@)
and then did an equalsIgnoreCase
to pare it with roin.
This string parsing approach works, but is there any better way to do this?
Share Improve this question edited Oct 17, 2012 at 17:39 Rohit Jain 213k45 gold badges414 silver badges533 bronze badges asked Oct 17, 2012 at 17:22 user1717230user1717230 4531 gold badge15 silver badges31 bronze badges 1-
4
Why to use regex? Take the 9 last character of the string and check if they are
@roin.
– SJuan76 Commented Oct 17, 2012 at 17:26
1 Answer
Reset to default 6You can try this pattern (\\S+?@roin\\.)
: -
\\S+
is used to match any non-space character?
after\\S+
is used to do reluctant matching. It will match least number of character to satisfy the pattern\\.
is used to match.
- Since
.
is a special character in Regex, that is why we need to escape it to match it as literal.
So, here's the code: -
String str = "[email protected]";
Pattern pattern = Pattern.pile("\\S+?@roin\\.");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
System.out.println("Matches"); // Prints this for this email
}