3

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]);
    }
}
4
  • 1
    \b must be \\b. Also, you need to use .find() with a Matcher object. It looks like you need to find all items that start with the pattern. If yes, use ^[A-Z]+\\.[0-9] Commented Jul 31, 2017 at 11:01
  • 2
    .matches will attempt to match complete input. Use: "[A-Z]+\\.[0-9]\\b.*" Commented Jul 31, 2017 at 11:02
  • The example you gave for arrayOfLine, can it contain values other than you mentioned ? Commented Jul 31, 2017 at 11:15
  • yes it can contain value other than i mention Commented Jul 31, 2017 at 11:19

3 Answers 3

7

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);
Sign up to request clarification or add additional context in comments.

2 Comments

if i add "2017 ." , "." , "$30","AVAILABLE.”" in arrayOfLine than these are also picked up by pattern.
@jagga: Please show me your code. My solution does not find them.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.