Java逻辑代码-从字符串中删除一些文本
我想从非常大的文本中删除任何出现的“很高兴”句子,忽略大小写。以下是一些大文本句子:
“她很高兴。我喜欢那个。”
“他快乐的儿子”
“他一整天都很快乐”
“塔莎很快乐”
“选择以下之一:伤心-高兴-哭”
我的初始代码是:
String largeText = "...."; // The very large text here.
String removeText = "is happy";
largeText = largeText.replaceAll( "(?i)" + removeText , "" );
此代码适用于第 1、3、4、5 句。但我不想将其从第 2 句中删除,因为它有其他含义。 我该怎么做?
I want to remove any occurence of "is happy" sentence from a very large text ignoring case sensitivity. Here are some of that large text sentences :
"She is happy. I like that."
"His happy son"
"He is happy all the day"
"Tasha is Happy"
"Choose one of the following: is sad-is happy-is crying"
My initial code is :
String largeText = "...."; // The very large text here.
String removeText = "is happy";
largeText = largeText.replaceAll( "(?i)" + removeText , "" );
This code will work fine with sentence number 1, 3, 4, 5. But i do not want to delete it from sentence number 2 as it has another meaning.
How can i do that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在模式周围使用
\b
来检测单词边界。 IE:Use
\b
around your pattern to detect word boundaries. ie:您可能想要研究原子零宽度断言 - 与字符串内的位置匹配的模式(例如单词边界),而不是文本本身。
这个问题以前曾被问过;有关详细信息,请参阅此链接:
java String.replaceAll 正则表达式问题
You might want to look into atomic zero-width assertions -- patterns that match against positions inside a string (such as a word boundary), rather than text itself.
This question was previously asked; see this link for more info:
java String.replaceAll regex question