How to use regex with String.matches in java

Viewed 8473

I have a String array

String[] arrayOfLine = {
    "I.2 Other Interpretive Provisions",
    "I.3 Accounting Terms",
    "Including all",
    "II.1 The Loans",
    "II.3 Prepayments.",
    "III.2 Illegality",
    "IV.2 Conditions",
    "V.2 Authorization",
    "expected to have"
};

I want to pick only those array elements which starts with roman.number i.e starting with I.2, II.1 and so on.

I am trying this, but it is not working

String regex = "\b[A-Z]+\\.[0-9]\b";
for (int i = 0; i < arrayOfLine.length; i++) {
    if(arrayOfLine[i].matches(regex)){
        listOfHeadings.add(arrayOfLine[i]);
    }
}
3 Answers
Related