Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import static java.util.Arrays.asList;
- public class Regex3 {
- private static final String EMAIL_REGEX = "(\\w+\\.\\w+|\\w+)(\\+\\w+)?@hogwarts(\\.\\w{2,3})+";
- public static void main(String[] args) {
- Regex3 regex = new Regex3();
- regex.shouldMatchEmailRegex();
- }
- private void shouldMatchEmailRegex() {
- List<String> shouldMatch = asList("tom@hogwarts.com", "tom.riddle@hogwarts.com",
- "tom.riddle+regexone@hogwarts.com", "tom@hogwarts.eu.com", "potter@hogwarts.com", "harry@hogwarts.com",
- "hermione+regexone@hogwarts.com");
- List<String> captureGroups = asList("tom", "tom.riddle", "tom.riddle", "tom", "potter", "harry", "hermione");
- shouldMatchRegexAndCaptures(EMAIL_REGEX, shouldMatch, captureGroups);
- }
- private void shouldMatchRegexAndCaptures(String regex, List<String> shouldMatch, List<String> captureGroups) {
- for (int i = 0; i < shouldMatch.size(); i++) {
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(shouldMatch.get(i));
- if (!matcher.matches()) {
- throw new IllegalStateException(shouldMatch.get(i) + " - Should match regex!");
- }
- if (!matcher.group(1).equals(captureGroups.get(i))) {
- throw new IllegalStateException(
- shouldMatch.get(i) + " - Should match - " + captureGroups.get(i) + " - capture group!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement