fork download
  1. import java.util.regex.*;
  2.  
  3. public class Main {
  4. private static Pattern combineRe(Pattern p1, Pattern p2){
  5. return Pattern.compile(combineRE(p1.pattern(), p2.pattern()));
  6. }
  7. private static String combineRE(String p1, String p2){
  8. int groups1 = 0, groups2=0;
  9. StringBuilder newP = new StringBuilder("(?=");
  10. newP.append(p1);
  11. newP.append("$)(?=");
  12.  
  13. Pattern capturingGroup = Pattern.compile("(?<!\\\\)(\\\\\\\\)*\\((?!\\?)");
  14. Matcher m = capturingGroup.matcher(p1);
  15. while(m.find()) groups1 ++;
  16.  
  17. m = capturingGroup.matcher(p2);
  18.  
  19. while(m.find()) groups2 ++;
  20. String new2 = p2;
  21.  
  22. for(int i=1; i<=groups2; i++)
  23. new2 = new2.replaceAll("(?<!\\\\)\\\\"+i, "\\\\" + (i+groups1));
  24.  
  25. newP.append(new2);
  26. newP.append("$).*");
  27. return newP.toString();
  28. }
  29. public static void main(String[] args){
  30. reComboTestCase[] cases = {
  31. new reComboTestCase(
  32. "(?=.*)([ab])\\1"
  33. , "([c]*)\\1bb"
  34. , new String[] { "aa" }
  35. , new String[] { "ccccbb" }
  36. , new String[] { "bb" }
  37. )
  38. , new reComboTestCase(
  39. "a."
  40. , ".b"
  41. , new String[] { "aa", "ac" }
  42. , new String[] { "bb", "cb"}
  43. , new String[] { "ab" }
  44. )
  45. };
  46.  
  47. for (int i=0; i<cases.length; i++)
  48. cases[i].test(i);
  49. }
  50. private static class reComboTestCase{
  51. String p1;
  52. String p2;
  53. String[] left; //Strings that should match p1 but not p2
  54. String[] right; //Strings that should match p2 but not p1
  55. String[] intersect; //Strings that should match both
  56. public reComboTestCase(String p1, String p2, String[] left, String[] right, String[] intersect) {
  57. this.p1 = p1;
  58. this.p2 = p2;
  59. this.left = left;
  60. this.right = right;
  61. this.intersect = intersect;
  62. }
  63. public void test(int n){
  64. System.out.printf("TEST %d:\n", n);
  65. System.out.printf("\t P1: %s\n", p1);
  66. System.out.printf("\t P2: %s\n", p2);
  67. String combo = Main.combineRE(p1,p2);
  68. System.out.printf("\tCombo: %s\n", combo);
  69. Pattern one = Pattern.compile(p1);
  70. Pattern two = Pattern.compile(p2);
  71. Pattern three = Pattern.compile(combo);
  72. System.out.printf("\t%100s: %5s %5s %5s\n", "LEFT", "ONE", "TWO", "COMBO");
  73. for(int i=0; i<left.length; i++)
  74. System.out.printf("\t%100s: %5s %5s %5s\n"
  75. , left[i]
  76. , one.matcher(left[i]).matches()
  77. , two.matcher(left[i]).matches()
  78. , three.matcher(left[i]).matches()
  79. );
  80. System.out.printf("\t%100s: %5s %5s %5s\n", "RIGHT", "ONE", "TWO", "COMBO");
  81. for(int i=0; i<right.length; i++)
  82. System.out.printf("\t%100s: %5s %5s %5s\n"
  83. , left[i]
  84. , one.matcher(right[i]).matches()
  85. , two.matcher(right[i]).matches()
  86. , three.matcher(right[i]).matches()
  87. );
  88. System.out.printf("\t%100s: %5s %5s %5s\n", "INTERSECT", "ONE", "TWO", "COMBO");
  89. for(int i=0; i<intersect.length; i++)
  90. System.out.printf("\t%100s: %5s %5s %5s\n"
  91. , left[i]
  92. , one.matcher(intersect[i]).matches()
  93. , two.matcher(intersect[i]).matches()
  94. , three.matcher(intersect[i]).matches()
  95. );
  96.  
  97. }
  98. }
  99. }
Success #stdin #stdout 0.09s 380544KB
stdin
Standard input is empty
stdout
TEST 0:
	   P1: (?=.*)([ab])\1
	   P2: ([c]*)\1bb
	Combo: (?=(?=.*)([ab])\1$)(?=([c]*)\2bb$).*
	                                                                                                LEFT:   ONE   TWO COMBO
	                                                                                                  aa:  true false false
	                                                                                               RIGHT:   ONE   TWO COMBO
	                                                                                                  aa: false  true false
	                                                                                           INTERSECT:   ONE   TWO COMBO
	                                                                                                  aa:  true  true  true
TEST 1:
	   P1: a.
	   P2: .b
	Combo: (?=a.$)(?=.b$).*
	                                                                                                LEFT:   ONE   TWO COMBO
	                                                                                                  aa:  true false false
	                                                                                                  ac:  true false false
	                                                                                               RIGHT:   ONE   TWO COMBO
	                                                                                                  aa: false  true false
	                                                                                                  ac: false  true false
	                                                                                           INTERSECT:   ONE   TWO COMBO
	                                                                                                  aa:  true  true  true