0

特定のテキストで始まり、特定のテキストで終わるJavaの文字列からすべてのサブ文字列を削除したい(例)

削除したいので

<tag> And everything in between  </endTag>

タグの間に終了行の文字があります。削除したいものは複数ありますが、そのうちの1つは

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が機能しないようなものでしょうか?

アップデート:

それは間違いなく私がそれらを削除した終了行の文字であり、それは機能します。しかし、私はまだ私のエンドラインの文字を保持したいのですが、それを行う方法はありますか?

4

2 に答える 2

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
于 2012-05-17T14:10:31.703 に答える
1

Javaではそのための正規表現を使用できます。正規表現を使用するメソッドの1つは、StringのreplaceAllメソッドです。

String s2= s.replaceAll("<b>.*?</b>", "");
于 2012-05-17T14:13:26.507 に答える