简体   繁体   English

如何从文本中删除字符串?

[英]How can I remove strings from text?

I have a long list of words that I want to remove from my text. 我有很多单词要从文本中删除。 For example, I have following text: 例如,我有以下文字:

blah blah search bla code blah will blah not blah help blah blah搜索bla代码等等等等等等等等等等等等等等

Now I want to remove the words, SEARCH, CODE, WILL, NOT, HELP from above string. 现在我想从上面的字符串中删除单词,SEARCH,CODE,WILL,NOT,HELP。

How can I do it easily in Java? 我怎样才能在Java中轻松完成?

I am trying the following code: 我正在尝试以下代码:

someString = someString.replaceAll("\\b(search|code|will|not|help)\\b", "");

However, I noticed that sometime it doesn't replace couple of words. 但是,我注意到有时它不会取代几个单词。 For example, I was using word "code" to replace but it didn't replace it (above small example works though). 例如,我使用单词“code”来替换它,但它没有替换它(虽然上面的小例子有效)。 I have huge files that I cannot copy past here :( 我有大量文件,无法复制到这里:(

Is there any other way to perform the same task? 还有其他方法可以执行相同的任务吗? or is there any way to find out what is causing this problem? 或者有什么方法可以找出造成这个问题的原因吗? Any test case? 任何测试用例?

Not sure I understand the question correctly. 不确定我是否正确理解了这个问题。 What's wrong with doing: 这样做有什么问题:

"blah blah search blah will blah not blah help".replaceAll("(search|will|not|help)", "") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM