4

I need to extract tuples out of string

e.g. (1,1,A)(2,1,B)(1,1,C)(1,1,D)

and thought some regex like:

String tupleRegex = "(\\(\\d,\\d,\\w\\))*";

would work but it just gives me the first tuple. What would be proper regex to match all the tuples in the strings.

M A
  • 65,721
  • 13
  • 123
  • 159
Vihaan Verma
  • 11,446
  • 17
  • 84
  • 120
  • "*it just gives me the first tuple*" Can we see your code? If I am not mistaken your regex should match entire `(1,1,A)(2,1,B)(1,1,C)(1,1,D)` and in group 1 it should contain *last* tuple `(1,1,D)`, not first one. – Pshemo Aug 02 '14 at 16:04

2 Answers2

6

Remove the * from the regex and iterate over the matches using a java.util.regex.Matcher:

String input = "(1,1,A)(2,1,B)(1,1,C)(1,1,D)";
String tupleRegex = "(\\(\\d,\\d,\\w\\))";
Pattern pattern = Pattern.compile(tupleRegex);
Matcher matcher = pattern.matcher(input);
while(matcher.find()) {
    System.out.println(matcher.group());
}

The * character is a quantifier that matches zero or more tuples. Hence your original regex would match the entire input string.

M A
  • 65,721
  • 13
  • 123
  • 159
2

One line solution using String.split() method and here is the pattern (?!^\\()(?=\\()

Arrays.toString("(1,1,A)(2,1,B)(1,1,C)(1,1,D)".split("(?!^\\()(?=\\()"))

output:

[(1,1,A), (2,1,B), (1,1,C), (1,1,D)]

Here is DEMO as well.

Pattern explanation:

  (?!                      look ahead to see if there is not:
    ^                        the beginning of the string
    \(                       '('
  )                        end of look-ahead
  (?=                      look ahead to see if there is:
    \(                       '('
  )                        end of look-ahead
Braj
  • 44,339
  • 5
  • 51
  • 69