我想从java中以特定文本开头并以特定文本结尾的字符串中删除所有子字符串(示例)
所以我想删除
<tag> And everything in between </endTag>
我在标签之间有终结字符。 我想删除多个内容,但其中一个以
开头WHAT DO YOU WANT TO KNOW ?
以
结束<end>
我试过了
text = text.replaceAll("WHAT DO YOU WANT TO KNOW \\?.*?<end>", "");
但它没有用
text = text.replaceAll("CHAPTER 18" , ""); works
这是一个我想要替换的文本块(只是一个例子,其中有更多的那些)(这是一本来自人类性行为的书中的一部分,所以如果你觉得不舒服但我觉得没有其中任何不合适的东西)
(Tons of text here) WHAT DO YOU WANT TO KNOW ?
Most kids today know all about sex at an early
age. So why are people so uptight about
showing nudity on television? What do they
think it will do to their kids?
Even in a society like ours, which has begun to discuss sex
more openly, it is still a diffi cult subject for children to
understand. Many parents believe that it is their job to
introduce the topic to their children, to explain it to them,
and to teach their children whatever values the parents
believe are appropriate. This may be undermined when
children see fairly uncensored sexuality on television, which
is usually shown without any discussion of values and
without any way to address the children’s questions about
what they are seeing. In the accompanying Sex in Real Life,
“Generation M,” we talk about research on the media
consumption habits of children and teenagers.
REALResearch > Studies have shown that people are less
likely to remember the brand name of a product in an ad with sex
and violence than in an ad without (BUSHMAN & BONACCI, 2002).
<end> (tons of text here)
这可能是我的文本格式化程序不允许replaceAll工作的方式吗?
更新
它绝对是终结线人物 我删除了它,它的工作原理。但我还是想保留我的终点字符是否有任何办法可以做到这一点?
答案 0 :(得分:2)
String s = "text that needs WHAT DO YOU WANT TO KNOW ? " +
"more text that needs deletion <end>to stay";
System.out.println(s.replaceAll("(?s)WHAT DO YOU WANT TO KNOW \\?.*?<end>", ""));
输出:
text that needs to stay
答案 1 :(得分:1)
您可以在Java中使用正则表达式。使用正则表达式的方法之一是String的replaceAll
方法:
String s2= s.replaceAll("<b>.*?</b>", "");