私はあなたが達成しようとしているかわかりませんが、バグがここにあります:
if(!m.group().equals(part))
{
str=m.replaceFirst(part);
}
マッチャはまだstr
の元の値で動作しているときは、str
を再割り当てされています。文字列は不変です。変数を1か所に再割当てすると、その変数を別の場所に変更しません(参照データ型引数の受け渡しのthis page of the Sun java Tutorialを参照)。
さらに奇妙なことが起こっていますが、おそらく私はあなたを正しく理解していません。 コメント内で、文字列がACCEPTで始まり、で終わると言います。ドット。しかし、それはPattern.compile("ACCEPT .*?\\.",Pattern.DOTALL);
を探している唯一のもので、あなたは何もキャプチャしていません。それで最初の場所で検索を気にかけないのはなぜですか?私はあなたが入力文字列がそうであることを知っていると思った。
実際に行うべきことは、いくつかのサンプル入力とそれから抽出するデータを投稿することです。そうしないと、誰もあなたを本当に助けることができません。
私はあなたのStringから複数のWITH句を削除したいと思うようです。トマト、パルメザンチーズ 、オリーブのパスタをACCEPT
:
String test =
"ACCEPT pasta "
+ "WITH tomatoes, parmesan cheese, olives "
+ "WITH anchovies WITH tuna WITH more olives.";
System.out.println(
test.replaceAll(
"(ACCEPT.*?WITH.*?)(?:\\s*WITH.*)(\\.)", "$1$2"
)
);
出力:これははるかに簡単に、このようなものでなければなりません。
ここではパターンがありますが、次のように説明し
final Matcher matcher =
Pattern.compile("WITH\\s*", Pattern.DOTALL).matcher(test);
final StringBuffer sb = new StringBuffer();
while(matcher.find()){
matcher.appendReplacement(sb, sb.length() == 0
? matcher.group()
: "");
}
matcher.appendTail(sb);
System.out.println(sb.toString());
出力:あなたの更新要件(WITHsを除去するが、引数を保つ)を考えると
( // start a capturing group
ACCEPT // search for the literal ACCEPT
.*? // search for the shortest possible matching String
// (so no other WITH can sneak in)
WITH // search for the literal WITH
.*? // search for the shortest possible matching String
// (so no other WITH can sneak in)
) // close the capturing group, we'll refer to this
// group as $1 or matcher.group(1)
(?: // start a non-capturing group
\\s* // search for optional whitespace
WITH // search for the literal WITH
.* // search for anything, greedily
) // close the group, we'll discard this one
( // open another capturing group
\\. // search for a single period
) // close the group, the period is now accessible as $2
をここで更新ソリューションです:
受け入れパスタトマト、パルメザンチーズ、オリーブは、より多くのオリーブを捕獲します。
入力文字列は何ですか? –
それは異なります..それはACCEPTで始まり、で終わります。ドット.. – GorillaApe
おそらくあなたはテスト文字列を追加して、あなたのコードに何をしたいのでしょうか? –