0

Below regex is working fine in most of the regex tools. However, its not working in the java code. Can anyone please advise?

String text="CHANGE FEE/ADD COLLECT DATA                                   "+
                "1.1  COLOR/RED TOMATO                                         "+
                "CF   USD10.00                                                 "+
                "                                                              "+
                "2.2  COLOR/DARK BLUE PLUM                                     "+
                "CF   USD11.00                                                 "+
                "                                                              ";
        String patterString = "([0-9]{1,3}\\.[0-9]{1,3})\\s.+\\s*CF\\s+[a-zA-Z]{1,5}([0-9]{1,10}.[0-9]{2})";
        Pattern pattern = Pattern.compile(patterString);  
        Matcher matcher = pattern.matcher(text);    

        while (matcher.find()) {    
            System.out.println("found: " + matcher.group(1) +">>>"+ matcher.group(2));
        }

actual output:

found: 1.1>>>11.00

expected output:

found: 1.1>>>10.00
found: 2.2>>>11.00
1

1 Answer 1

1

Your regex needs to be:

String patterString = "([0-9]{1,3}\\.[0-9]{1,3}).*?CF\\s+[a-zA-Z]{1,5}([0-9]{1,10}.[0-9]{2})";

Which yields:

found: 1.1>>>10.00
found: 2.2>>>11.00

I haven't read the docs, but guess that when iterating with find() it's implicitly in MULTILINE mode, so the portion of your regex \\s.+\\s* is greedy - replacing this with .*? minimizes the greed ;-)


Edit, sample source:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexFind {

    public static void main(String[] args)
    {
        String text="CHANGE FEE/ADD COLLECT DATA                                   "+
                "1.1  COLOR/RED TOMATO                                         "+
                "CF   USD10.00                                                 "+
                "                                                              "+
                "2.2  COLOR/DARK BLUE PLUM                                     "+
                "CF   USD11.00                                                 "+
                "                                                              ";
        //String patterString = "([0-9]{1,3}\\.[0-9]{1,3})\\s.+\\s*CF\\s+[a-zA-Z]{1,5}([0-9]{1,10}.[0-9]{2})";
        String patterString = "([0-9]{1,3}\\.[0-9]{1,3}).*?CF\\s+[a-zA-Z]{1,5}([0-9]{1,10}.[0-9]{2})";

        Pattern pattern = Pattern.compile(patterString);
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {
            System.out.println("found: " + matcher.group(1) +">>>"+ matcher.group(2));
        }
    }
}
Sign up to request clarification or add additional context in comments.

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.