2

String クラスのメソッドで使用できる正規表現が必要で、末尾にあるものを除いてreplaceAllすべてのインスタンスを置き換えます*.*\

つまり、変換は

[any character]*[any character] => [any character].*[any character]
* => .*
\* => \* (i.e. no conversion.)

誰か助けてくれませんか?

4

2 に答える 2

4

後読みを使用します。

String resultString = subjectString.replaceAll("(?<!\\\\)\\*", ".*");

説明 :

"(?<!" +     // Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
   "\\\\" +       // Match the character “\” literally
")" +
"\\*"         // Match the character “*” literally
于 2011-11-17T08:58:02.600 に答える
1

キャプチャ グループなしで実行できる場合もありますが、これは機能するはずです。

myString.replaceAll("\\*([^\\\\]|$)", "*.$1");
于 2011-11-17T08:56:56.367 に答える