### Question:
RegexPermuter { public static Collection<String> permute(String regexp) { regexp = regexp.replaceAll("\\)\\?([^()]+)\\(", ")($1)("); regexp = regexp.replaceAll("\\)([^?][^()]*)\\(", ")($1)("); List<String> bracketMatches = StringHelper.getRegexpMatches("\\(.*?\\)\\??", regexp); List<String[]> bracketSplits = new ArrayList<>(); for (String bracket : bracketMatches) { bracket = bracket.replace("(", "").replace(")", ""); if (bracket.endsWith("?")) { bracket = bracket.replace("?", "").trim(); bracket += "|_"; } String[] splits = bracket.split("\\|"); bracketSplits.add(splits); } List<String> permutations = new ArrayList<>(); permuteRecursively(permutations, bracketSplits, ""); return permutations; }  static Collection<String> permute(String regexp);  }### Answer:
@Test public void testPermute() throws Exception { Collection<String> permute = RegexPermuter.permute("(a|b)(d|e)"); collector.checkThat(permute, CoreMatchers.hasItems("ad","ae","bd","be")); permute = RegexPermuter.permute("(a|b)?(d|e)"); collector.checkThat(permute, CoreMatchers.hasItems("ad","ae","bd","be","d","e")); collector.checkThat(permute.size(), is(6)); permute = RegexPermuter.permute("(a|b)?(d|ds|e)"); collector.checkThat(permute, CoreMatchers.hasItems("ad","ae","bd","be","d","e")); collector.checkThat(permute.size(), is(9)); permute = RegexPermuter.permute("(how|what) (is|are) (you|strange)"); collector.checkThat(permute, CoreMatchers.hasItems("what is strange","how are you","how is strange")); collector.checkThat(permute.size(), is(8)); }