以下では、正確な検索文字列を置換文字列xxxに置き換えようとしています。Javaログメッセージに似たオリジナルの文字列があります。検索文字列が「変数」であるかどうかを確認できるように、xxxに正常に置き換えることができますが、検索文字列が「variable.method()」または「variable.method2()」の場合は置き換えることができません。私はここで何か間違ったことをしていますか?
val original = """variable+ " is in " + variable.method() + " and " + variable.method2()"""
val replaceStr = "xxx"
val searchStr = "variable.method()" // This can change like in the output below
val variableExtracter = ("(?<=^|\\s|\\+)"+searchStr+"(?=\\s|$|\\+)").r
print("Search string : " + searchStr + "\n")
print("Original :" + original + "\n")
val formatted = variableExtracter.replaceAllIn(original, replaceStr)
print("Formatted :" + formatted + "\n")
Output:
Search string : variable //CORRECT OUTPUT
Original :variable+ " is in " + variable.method() + " and " + variable.method2()
Formatted :xxx+ " is in " + variable.method() + " and " + variable.method2()
---
Search string : variable.method()
Original :variable+ " is in " + variable.method() + " and " + variable.method2()
Formatted :variable+ " is in " + variable.method() + " and " + variable.method2()
//EXPECTED OUTPUT: variable+ " is in " + xxx + " and " + variable.method2()
---
Search string : variable.method2()
Original :variable+ " is in " + variable.method() + " and " + variable.method2()
Formatted :variable+ " is in " + variable.method() + " and " + variable.method2()
//EXPECTED OUTPUT:variable+ " is in " + variable.method() + " and " + xxx
---
Search string : variable.method()
Original :variable.method()+ " has " + variable + " and " + variable.method2()
Formatted :variable.method()+ " has " + variable + " and " + variable.method2()
//EXPECTED OUTPUT:xxx+ " has " + variable + " and " + variable.method2()