如何从源在java中
问题描述:
删除一个正则表达式匹配我有一个字符串,如下所示:如何从源在java中
Acid Exposure (pH) Total
Total Normal
Clearance pH : Channel 7
Number of Acid Episodes 6
Time 48.6 min
Percent Time 20.3%
Mean Acid Clearance Time 486 sec
Longest Episode 24.9 min
Gastric pH : Channel 8
Time pH<4.0 208.1 min
Percent Time 86.7%
Postprandial Data (Impedance) Total
Total Normal
Acid Time 2.9 min
Acid Percent Time 1.2%
Nonacid Time 11.6 min
Nonacid Percent Time 4.8%
All Reflux Time 14.5 min
All Reflux Percent Time 6.1%
Median Bolus Clearance Time 8 sec
Longest Episode 11.2 min
NOTE: Reflux episodes are detected by Impedance and categorized as acid or nonacid by pH
我想从Bolus Exposure (Impedance)
总删除对
NOTE: Reflux episodes are detected by Impedance and categorized as acid or nonacid by pH
一切我的代码是
Pattern goPP = Pattern.compile("Postprandial Data.*?Reflux episodes are detected by Impedance and categorized as acid or nonacid by pH",Pattern.DOTALL);
Matcher goPP_pattern = goPP.matcher(s);
while (goPP_pattern.find()) {
for (String df:goPP_pattern.group(0).split("\n")) {
s.replaceAll(df,"");
}
}
然而,字符串s与此前相同。我如何从源字符串中删除匹配项?如果这是不可能的,我怎样才能创建一个新的字符串,但只有匹配
答
字符串在Java中是不可变的,请更改以下代码以进行赋值。
s.replaceAll(df,""); // wrong, no op
s = s.replaceAll(df,"");//correct
答
为什么不使用String.replaceAll?
s = s.replaceAll(
"Postprandial Data.*?Reflux episodes are detected by Impedance and categorized as acid or nonacid by pH",
"Postprandial Data\nReflux episodes are detected by Impedance and categorized as acid or nonacid by pH"
);
答
尽量简单
s = s.replaceAll("(?s)Postprandial Data.*?Reflux episodes are detected by Impedance and categorized as acid or nonacid by pH", "");
注:(?s)
是DOTALL选项。