私は次のような文字列を持っています
「茶色のキツネが@フェンス@を飛び越えた」
'@'であり、2 つの間のすべての部分文字列を' 'に置き換えたいと考えていkickedます。つまり、最終的な出力は次のようになります。
「茶色のキツネは蹴られたものを蹴った」
次のように書きましたが、間違いがわかりません。
string.replaceAll("^@.*@$", "kicked");
You should not use anchor elements ^ and $. They mean the beginning and the end of the entire input, not the beginning and the ending of the word. You should also replace the dot . with [^@] (meaning "anything but @") to make your expression more efficient.
string.replaceAll("@[^@]*@", "kicked");
If you would like to avoid replacing tagged elements inside a word, e.g. if you want to preserve he@ll@o as is, rather than making it hekikkedo, you can put in markers of word boundaries \b on both ends of the expression:
string.replaceAll("\\b@[^@]*@\\b", "kicked");