I use Java to process text using regular expressions. I use the following regex
^[\([0-9a-zA-Z]+\)\s]+
to combine one or more letters or numbers in parentheses one or more times. For example, I like to match (aaa) (bb) (11) (AA) (iv) or (111) (aaaa) (i) (V)
I checked this regex at http://java-regex-tester.appspot.com/ and it works. But when I use it in my code, the code does not compile. Here is my code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tester {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[\([0-9a-zA-Z]+\)\s]+");
String[] words = pattern.split("(a) (1) (c) (xii) (A) (12) (ii)");
String w = pattern.
for(String s:words){
System.out.println(s);
}
}
}
I tried to use \ instead of \, but the regular expression gave different results than expected (it corresponds to only one group, for example (aaa), and not to several groups, such as (aaa) (111) (ii).
Two questions:
- ?
- (, (aaa), (111) ..). pattern.split, .