RegularExpressionConverter { public static Pattern convertPerlRegexToPattern(@Nonnull final String regex) { return convertPerlRegexToPattern(regex, false); } private  RegularExpressionConverter(); static String convertPatternToPerlRegex(@Nonnull final Pattern pattern); static Pattern convertPerlRegexToPattern(@Nonnull final String regex); static Pattern convertPerlRegexToPattern(@Nonnull final String regex, @Nonnull final boolean faultTolerant);  }
@Test public void convertPerlRegexToPattern_faultTolerantModifierRegexConverting_NintendoDS() { final String perlStyleRegex = "/Nintendo DS/Si"; final Pattern pattern = RegularExpressionConverter.convertPerlRegexToPattern(perlStyleRegex, true); final Pattern expected = Pattern.compile("Nintendo DS", Pattern.CASE_INSENSITIVE); assertThat(pattern.flags()).isEqualTo(expected.flags()); }
@Test(expected = IllegalArgumentException.class) public void convertPerlRegexToPattern_NintendoDS() { final String perlStyleRegex = "/Nintendo DS/Si"; RegularExpressionConverter.convertPerlRegexToPattern(perlStyleRegex); }
@Test(expected = IllegalNullArgumentException.class) public void convertPerlRegexToPattern_null() { RegularExpressionConverter.convertPerlRegexToPattern(null); }
@Test public void convertPerlRegexToPattern_regexWithApparentlyIntersection() { final String perlStyleRegex = "/test/([0-9a-zA-Z.\\-+]+)/s"; final Pattern pattern = RegularExpressionConverter.convertPerlRegexToPattern(perlStyleRegex); final Pattern expected = Pattern.compile("test/([0-9a-zA-Z.\\-+]+)", Pattern.DOTALL); assertThat(pattern.flags()).isEqualTo(expected.flags()); assertThat(pattern.pattern()).isEqualTo(expected.pattern()); }
@Test public void convertPerlRegexToPattern_regexWithModifiers() { final String perlStyleRegex = "/\\s*[a-zA-Z0-9]*/im"; final Pattern pattern = RegularExpressionConverter.convertPerlRegexToPattern(perlStyleRegex); final Pattern expected = Pattern.compile("\\s*[a-zA-Z0-9]*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); assertThat(pattern.flags()).isEqualTo(expected.flags()); assertThat(pattern.pattern()).isEqualTo(expected.pattern()); }
@Test public void convertPerlRegexToPattern_regexWithWhitespace() { final String perlStyleRegex = "/\\s*/ [a-zA-Z0-9]* /im "; final Pattern pattern = RegularExpressionConverter.convertPerlRegexToPattern(perlStyleRegex); final Pattern expected = Pattern.compile("\\s*/ [a-zA-Z0-9]* ", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); assertThat(pattern.flags()).isEqualTo(expected.flags()); assertThat(pattern.pattern()).isEqualTo(expected.pattern()); }
@Test public void convertPerlRegexToPattern_simpleRegex() { final String perlStyleRegex = "/\\s*[a-zA-Z0-9]*/"; final Pattern pattern = RegularExpressionConverter.convertPerlRegexToPattern(perlStyleRegex); final Pattern expected = Pattern.compile("\\s*[a-zA-Z0-9]*"); assertThat(pattern.flags()).isEqualTo(expected.flags()); }
@Test public void convertPerlRegexToPattern_swiftfoxRegex() { final String perlStyleRegex = "/mozilla.*rv:[0-9\\.]+.*gecko\\/[0-9]+.*firefox\\/([0-9a-z\\+\\-\\.]+).*swiftfox/si"; RegularExpressionConverter.convertPerlRegexToPattern(perlStyleRegex); final Pattern pattern = RegularExpressionConverter.convertPerlRegexToPattern(perlStyleRegex, true); final Pattern expected = Pattern.compile("mozilla.*rv:[0-9\\.]+.*gecko\\/[0-9]+.*firefox\\/([0-9a-z\\+\\-\\.]+).*swiftfox", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); assertThat(pattern.flags()).isEqualTo(expected.flags()); }