String クラスのメソッドで使用できる正規表現が必要で、末尾にあるものを除いてreplaceAll
すべてのインスタンスを置き換えます*
.*
\
つまり、変換は
[any character]*[any character] => [any character].*[any character]
* => .*
\* => \* (i.e. no conversion.)
誰か助けてくれませんか?
String クラスのメソッドで使用できる正規表現が必要で、末尾にあるものを除いてreplaceAll
すべてのインスタンスを置き換えます*
.*
\
つまり、変換は
[any character]*[any character] => [any character].*[any character]
* => .*
\* => \* (i.e. no conversion.)
誰か助けてくれませんか?
後読みを使用します。
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
キャプチャ グループなしで実行できる場合もありますが、これは機能するはずです。
myString.replaceAll("\\*([^\\\\]|$)", "*.$1");