-4

Given the string "abc xyz def", i want both the strings "abc def" and "def abc" to give a successful match, how can i accomplish this with a regex in Java?

More: The optimal solution maybe would be something similar to the fuzzy search provided by the regex module in python to provide also additional capabilities to the regex.

K-FLOW
  • 135
  • 1
  • 10

2 Answers2

0

What you want is called "groups" in regex. Groups are placed in parenthesis () and can be literal or symbolised regex syntax for types of stipulated characters. java.util.Scanner and other associate Iterator interfaces and classes or java.lang.String and the main package java.util.regex for regular expressions contain tools for group capturing.

String abc = null; // weird but true for building regex into the expression on the fly in a re-callable method
String abc = new String("abc");
// hazard a guess such a data ***mining*** techniques would appear like this to capture groups whether present of not
Pattern p =  "((\b)*"+abc+"(\b)*)*   |   ((\b)*xyz(\b)*)* |  ((\b)*def(\b)*)*";
Samuel Marchant
  • 331
  • 2
  • 6
-1

Hope This can help you:

    String regex1 = "(.*)abc(.*)";
    String regex2 = "(.*)def(.*)";
    String s = "abc xyz def";
    if (Pattern.matches(regex1, s) & (Pattern.matches(regex2, s) ) ) {
        System.out.println("Correct");
    }