我有一个变量字符串,当字符串在单词边界处作为一个整体匹配时,应该替换它.
我在这个正则表达式上取得了一些成功:
\b\QExample String to replace\E
或在Kotlin实施:
string.replace("""\b${Regex.escape(toReplace)}""".toRegex(), replacement)
然而,当toReplace
类似于[template] Example String to replace
时,这不起作用.
我想原因是,[
不被视为"单词",因此与\b
不匹配.