jagga
jagga

Reputation: 163

How to use regex with String.matches in java

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]);
    }
}

Upvotes: 3

Views: 25747

Answers (3)

Rohit-Pandey
Rohit-Pandey

Reputation: 2159

Here is regex for checking roman number with dot is ^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})[.].

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class test
    {
     public static void main(String[] args) {
     String[] a = {
     "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"
     };
     int i=0;
     int b=a.length;
     Pattern MY_PATTERN = Pattern.compile("^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})[.]");
     for(i=0;i<b;i++)
     {
         Matcher m = MY_PATTERN.matcher(a[i]);
         if (m.find())
             System.out.println(a[i]);
     }
     }
 }

Output:

I.2 Other Interpretive Provisions
I.3 Accounting Terms
II.1 The Loans
II.3 Prepayments.
III.2 Illegality
IV.2 Conditions
V.2 Authorization

Upvotes: 0

Symph
Symph

Reputation: 36

You sould try using Patterns and Matchers. Here is a working example

    Pattern pattern = Pattern.compile("[A-Z]+\\.[0-9]");
    for (int i = 0; i < arrayOfLine.length; i++) {
        Matcher match = pattern.matcher(arrayOfLine[i]);
        while (match.find()) {
            listOfHeadings.add(match.group());
        }
    }

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

It looks like you need to find all items that start with the pattern. Use "^[A-Z]+\\.[0-9]+\\b" pattern and make sure you run the find() method of the Matcher object to find partial matches inside strings. .matches() finds the entire string matches only. Note that \b word boundary must be defined as "\\b" inside a Java string literal.

See the Java demo

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"};
Pattern pat = Pattern.compile("^[A-Z]+\\.[0-9]+\\b");
List<String> listOfHeadings = new ArrayList<>();
for (String s : arrayOfLine) {
    Matcher m = pat.matcher(s);
    if (m.find()) {
        listOfHeadings.add(s);
    }
}
System.out.println(listOfHeadings);

Upvotes: 7

Related Questions