结束行字符阻止java从字符串中删除子字符串

结束行字符阻止java从字符串中删除子字符串,java,Java,我想删除java中以特定文本开头、以特定文本结尾的字符串的所有子字符串(示例) 所以我想删除 <tag> And everything in between </endTag> 以 <end> 这是我想替换的一段文字(只是一个例子,还有更多的例子)(这是一本来自《人类性学》的书,所以如果你觉得不舒服,不要读它,但我觉得里面没有任何不合适的东西) (这里有大量文本)你想知道什么? 今天的大多数孩子很早就知道性的一切 年龄。那么,为什么人们对这件事如此紧张呢

我想删除java中以特定文本开头、以特定文本结尾的字符串的所有子字符串(示例)

所以我想删除

<tag> And everything in between  </endTag>

<end>
这是我想替换的一段文字(只是一个例子,还有更多的例子)(这是一本来自《人类性学》的书,所以如果你觉得不舒服,不要读它,但我觉得里面没有任何不合适的东西)

(这里有大量文本)你想知道什么?
今天的大多数孩子很早就知道性的一切
年龄。那么,为什么人们对这件事如此紧张呢
在电视上显示裸体?它们是什么
你认为这会对他们的孩子有什么影响?
即使在我们这样一个已经开始讨论性的社会里
更坦率地说,这仍然是孩子们难以接受的话题
明白。许多家长认为,他们的工作是
向他们的孩子介绍这个话题,向他们解释,
教他们的孩子任何父母看重的东西
我认为这是恰当的。这可能会在以下情况下受到破坏:
孩子们在电视上看到未经审查的性行为,这
通常显示时不讨论值和
没有办法解决孩子们关于
他们所看到的。在现实生活中伴随着性,
“M一代”,我们谈论关于媒体的研究
儿童和青少年的消费习惯。
REALResearch>研究表明,人们的
很可能记得性广告中产品的品牌名称
和暴力相比,广告中没有暴力(BUSHMAN&BONACCI,2002)。
(此处有大量文本)
可能是因为我的文本格式化程序不允许replaceAll工作吗

更新:

它明确地定义了结束行字符
我移除了它们,它就工作了。但我仍然想保留我的结束行字符,有什么方法可以做到吗

您可以在Java中使用正则表达式。使用正则表达式的方法之一是String的
replaceAll
方法:

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>", ""));
String s2= s.replaceAll("<b>.*?</b>", "");
String s2=s.replaceAll(“*”,“);

您可以在Java中使用正则表达式。使用正则表达式的方法之一是String的
replaceAll
方法:

String s2= s.replaceAll("<b>.*?</b>", "");
String s2=s.replaceAll(“*”,“);

当我将您的代码复制粘贴到我的java中时,它可以工作。但是当我对字符串应用相同的东西时:text=text.replaceAll(“您想知道什么\\?.*”,”);它不起作用。中间的结束行字符与此有关吗??或者我的绳子太长了?还是没有雪茄。我觉得这与我的文本格式有关。@Cod13新行肯定会改变故事。尝试新的表达式(在前面添加了
(?s)
——启用dotall匹配模式)。Omg谢谢。谢谢你改变了你的问题。我只是觉得我做了一些完全错误的事情,因为我是从一个飞跃的缺席回到java。谢谢你坚持我的想法,当我把你的代码复制粘贴到我的java中时,它就工作了。但是当我对字符串应用相同的东西时:text=text.replaceAll(“您想知道什么\\?.*”,”);它不起作用。中间的结束行字符与此有关吗??或者我的绳子太长了?还是没有雪茄。我觉得这与我的文本格式有关。@Cod13新行肯定会改变故事。尝试新的表达式(在前面添加了
(?s)
——启用dotall匹配模式)。Omg谢谢。谢谢你改变了你的问题。我只是觉得我做了一些完全错误的事情,因为我是从一个飞跃的缺席回到java。谢谢你和我在一起
 (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) 
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
String s2= s.replaceAll("<b>.*?</b>", "");