Rahul Kulhalli Rahul Kulhalli - 1 month ago 5
Java Question

What kind of regex should I use for the following template?

I'm writing a Java program that calculates a person's average salary, and prints the monthly deductions in a template. The template is something like this:

==============BILL==============
| NAME: xXxX BRANCH : xxx |
| |
| Month 1 : xxx.xxx |
| Month 2 : xxxx.xx |
| <other Months> |
| Month 12 : xxx.xx |
| |
| TOTAL : ____________ |
================================


I am using the following pattern to try and capture the elements:

//template is stored in string.
String[] lines = msg.split("\n");
Pattern p = Pattern.compile("[xX\\._]+");
for(String line : lines){
Matcher m = p.matcher(line);
if(m.find()){
System.out.println(m.group());
}
else{
System.out.println("no match found...");
}
}


The output I'm getting is something like this:

xXxX
xxx.xxx
xxxx.xx
xxx.xx
____________


However, I'm not able to match the 'xxx' of BRANCH. How do I extract that pattern?

Answer

Change

if(m.find()){
    System.out.println(m.group());
}

to

while(m.find()){
        System.out.println(m.group());
}

as NAME and BRANCH are on the same line.

Matcher#find() will find first match in string, not all matches. To get all matches you have to call find() multiple times.

Comments