@Test public void testAcceptStrings() { String[] input = new String[3]; input[0] = "foo"; input[1] = "bar"; input[2] = "baz"; String regex = PatternGenerator.acceptStrings(input); Pattern pattern = Pattern.compile(regex); assertTrue(pattern.matcher("foo").matches()); assertTrue(pattern.matcher("bar").matches()); assertTrue(pattern.matcher("baz").matches()); assertFalse(pattern.matcher("foobaz").matches()); assertFalse(pattern.matcher("").matches()); }
@Nonnull public static String acceptStrings(@Nonnull String[] input) { StringBuilder builder = new StringBuilder(); builder.append("("); for (int i = 0; i < input.length; i++) { builder.append(Pattern.quote(input[i])); if (i < input.length - 1) { builder.append("|"); } } builder.append(")"); return builder.toString(); }
PatternGenerator { @Nonnull public static String acceptStrings(@Nonnull String[] input) { StringBuilder builder = new StringBuilder(); builder.append("("); for (int i = 0; i < input.length; i++) { builder.append(Pattern.quote(input[i])); if (i < input.length - 1) { builder.append("|"); } } builder.append(")"); return builder.toString(); } }
PatternGenerator { @Nonnull public static String acceptStrings(@Nonnull String[] input) { StringBuilder builder = new StringBuilder(); builder.append("("); for (int i = 0; i < input.length; i++) { builder.append(Pattern.quote(input[i])); if (i < input.length - 1) { builder.append("|"); } } builder.append(")"); return builder.toString(); }  }
PatternGenerator { @Nonnull public static String acceptStrings(@Nonnull String[] input) { StringBuilder builder = new StringBuilder(); builder.append("("); for (int i = 0; i < input.length; i++) { builder.append(Pattern.quote(input[i])); if (i < input.length - 1) { builder.append("|"); } } builder.append(")"); return builder.toString(); }  @Nonnull static String acceptStrings(@Nonnull String[] input); }
PatternGenerator { @Nonnull public static String acceptStrings(@Nonnull String[] input) { StringBuilder builder = new StringBuilder(); builder.append("("); for (int i = 0; i < input.length; i++) { builder.append(Pattern.quote(input[i])); if (i < input.length - 1) { builder.append("|"); } } builder.append(")"); return builder.toString(); }  @Nonnull static String acceptStrings(@Nonnull String[] input);  }