2

重複の可能性:
正規表現は大文字と小文字をすべて無視して置換

必要な出力を取得するにはどうすればよいですか?

入力文字列 -

"Software Industry. software Industry. SOFTWARE INDUSTRY. software industry. "

検索する単語 -

"software industry"

出力 -

"*Software Industry*. *software Industry*. *SOFTWARE INDUSTRY*. *software industry*. "

要するに、フレーズを見つけて、同じフレーズを開始と終了のアスタリスク (*) に置き換え、大文字と小文字を無視する必要があります..

4

3 に答える 3

3

これには、パターン マッチング (正規表現) を使用できます。

最初にグループを使用してフレーズを抽出し、文字列またはその他のデータ構造に格納します。次に、Replace 関数を使用して出力を実現します。

この投稿を参照できます: http://www.vogella.com/articles/JavaRegularExpressions/article.html

于 2012-10-17T06:25:08.533 に答える
3

どちらで置き換えるかreplaceAllを使用できます。(?i)case-insensitive

String str = "Software Industry. software Industry. SOFTWARE INDUSTRY. "+
                                                    "software industry. "
str = str.replaceAll("(?i)(software industry)", "\\*$1\\*");
于 2012-10-17T06:25:21.897 に答える
2

次のようなものを試すこともできます。

String str=new String("Software Industry. software Industry. SOFTWARE INDUSTRY. software industry.");
str=Pattern.compile("(software industry)",Pattern.CASE_INSENSITIVE).matcher(str).replaceAll("*$1*");
于 2012-10-17T06:27:30.507 に答える