如何在 groovy 中将“<space>”替换为“\\<space>”

How to replace "<space>" with "\\<space>" in groovy

尝试过

def resultString="Hello word result Hello: 0 Word: 0 void: 0"
def resultString1=resultString.replaceAll(' ', '\\ ')
println resultString1

预期结果:

Hello\ word\ result\ Hello:\ 0\ Word:\ 0\ void:\ 0

实际获得:

Hello\ word\ result\ Hello:\ 0\ Word:\ 0\ void:\ 0

任何输入都会非常有用

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-
replaceAll 使用正则表达式匹配,它改变了转义的行为

改用replace

def resultString="Hello word result Hello: 0 Word: 0 void: 0"
def resultString1=resultString.replace(' ','\\')
println resultString1

输出:

Hello\word\result\Hello:\0\Word:\0\void:\0

def resultString="Hello word result Hello: 0 Word: 0 void: 0" println(resultString.replaceAll("\s","\\\\"))