首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >replaceAll在groovy中没有长句工作

replaceAll在groovy中没有长句工作
EN

Stack Overflow用户
提问于 2020-04-24 13:06:50
回答 1查看 89关注 0票数 0

我有这个段落(这只是一个随机的文本),我想删除这一段的最后一部分。我试过使用replaceAll,但它只适用于较短的句子,例如,引起新冠肺炎的病毒

但我想移除:如果你在接近新冠肺炎的人附近,或者接触受污染的表面,你的眼睛、鼻子或嘴巴就会感染上。\n大多数人会在没有特殊治疗的情况下,经历轻微到温和的症状,并康复。

“引起新冠肺炎的病毒主要是通过感染者咳嗽、打喷嚏或呼出时产生的飞沫传播的。这些飞沫太重,无法悬挂在空气中,很快就会落在地板或表面上。\n\n\n如果你靠近感染了新冠肺炎的人,或者接触受污染的表面,然后触摸受污染的表面,然后触摸眼睛、鼻子或嘴巴,你就会感染上这种病毒。\n\n大多数患有流感的人会在没有特殊治疗的情况下,经历轻微到温和的症状,并恢复健康。”

代码语言:javascript
复制
  String comment = "The virus that causes COVID-19 is mainly transmitted through droplets generated when an infected person coughs, sneezes, or exhales. These droplets are too heavy to hang in the air, and quickly fall on floors or surfaces.\n\nAlso, SOS stands for Style Or Service and we?re here to help you with all your shopping needs. Feel free to contact us at 1.877.765.3009 or e-mail us at sos@solesociety.com. #mysolesociety Have a great day! Feel free to contact us at 855-435-5050 or customerservice@vincecamuto.com if you have any further questions or concerns. Have a great day!"

//produces the right result.
println(comment.replaceAll("Also, SOS stands", " ")) 
代码语言:javascript
复制
//this does not produce the right result, instead it prints the entire paragraph with this part included.
println(comment.replaceAll("Also, SOS stands for Style Or Service and we?re here to help you with all your shopping needs. Feel free to contact us at 1.877.765.3009 or e-mail us at sos@solesociety.com. #mysolesociety Have a great day! Feel free to contact us at 855-435-5050 or customerservice@vincecamuto.com if you have any further questions or concerns. Have a great day!", " ")) 

请帮助我们就如何解决这个问题提出建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-24 14:02:56

字符?在正则表达式中具有特殊的意义。当你真的想把它包括进去的时候,你必须掩盖它。

例如,使用\\?而不是?

或者用\\Q\\E包围整个字符串,这意味着引用字符串(即"\\Qstring with question marks? This works?\\E" )。

另一个解决方案是使用Pattern.quote(pattern),因为它掩盖了一切,包括\\Q\\E

如果不需要正则表达式进行模式匹配,最好使用comment.replace(target, replacement)。这比使用regex更快。

看看javadoc!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61409105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档