【发布时间】:2016-10-06 05:52:22
【问题描述】:
我正在编写一个 Java 程序来计算一个人的平均工资,并在模板中打印每月扣除额。模板是这样的:
==============BILL==============
| NAME: xXxX BRANCH : xxx |
| |
| Month 1 : xxx.xxx |
| Month 2 : xxxx.xx |
| <other Months> |
| Month 12 : xxx.xx |
| |
| TOTAL : ____________ |
================================
我正在使用以下模式来尝试捕获元素:
//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...");
}
}
我得到的输出是这样的:
xXxX
xxx.xxx
xxxx.xx
xxx.xx
____________
但是,我无法匹配 BRANCH 的“xxx”。如何提取该模式?
【问题讨论】: