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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.