我希望删除行b / w"&你好Buddy"和"& endmarker&"包括的。我使用了如下所示的replaceAll,并且没有任何帮助......
val s1 =
"""
|& Hello Buddy
|------------------------------
|Hello;
|GO
|My
|use &endmarker&
| GO
|World
|go
|, I am Naga
|+++++++++++++++++++++++++
|GOTO School
|GO Heaven
"""
val rg =s1.replaceAll("(?m)&(.*)&endmarker&" ," ")
答案 0 :(得分:2)
我建议
val rg =s1.stripMargin('|').replaceAll("(?s)& Hello Buddy(.*?)&endmarker&" ," ").trim()
输出:
GO
World
go
, I am Naga
+++++++++++++++++++++++++
GOTO School
GO Heaven
请参阅Scala demo
.stripMargin('|')
将摆脱缩进。
模式详情
(?s)
- DOTALL修饰符,.
匹配任何字符,包括换行符& Hello Buddy
- 文字字符串.*?
- 任意0个字符尽可能少,直到第一次出现...... &endmarker&
- 文字字符串。如果您要处理大文件,则应重新编写该模式以获得更好的性能
"(?s)& Hello Buddy[^&]*(?:&(?!endmarker&)[^&]*)*&endmarker&"
其中.*?
lazy dot被替换为匹配的展开模式[^&]*(?:&(?!endmarker&)[^&]*)*
[^&]*
- 除&
(?:&(?!endmarker&)[^&]*)*
- 零次或多次出现:
&(?!endmarker&)
- &
未跟endmarker&
[^&]*
- 除&