2012-05-17 176 views
0

去除子我想从在java中一个字符串,它与特定的文本开始除去所有子串和具有特定文本结尾(例如)结束行字符防止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工作?

UPDATE:

其definetly结束行字符 我删除它们,它的工作原理。但我仍然想保留我的终结线字符有什么办法可以做到这一点?

回答

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 
+0

当我复制粘贴你的代码到我的java它的作品。但是,当我将相同的东西应用于我的字符串时:text = text.replaceAll(“你想知道什么\\?。* ”,“”);它不起作用。终结线字符之间有什么关系呢?或者我的字符串太长了? – Xitcod13

+0

还没有雪茄。我觉得这与我的文本格式有关。 – Xitcod13

+0

@ Xitcod13 Newlines绝对改变了这个故事。尝试新的表达式(将'(?s)'添加到前端启用dotall匹配模式)。 –

1

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

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