我有一个 XPath
//*[@title='ab'cd']
我想将其输出为
//*[@title='ab\'cd']
我正在使用此代码
property = property.replaceAll("^[a-zA-Z]+(?:'[a-zA-Z]+)*", "\'");
但是正在输出
//*[@text='ab'cd']
我在 StackOverflow 上找不到类似的问题。如果有,请在评论中发布链接。
最佳答案
替换 '
在两个字母之间你需要一个 (?<=\p{L})'(?=\p{L})
正则表达式。
一个(?<=\p{L})
是一个正向后查找,需要紧邻当前位置左侧的一个字母,并且 (?=\p{L})
是一个正向前瞻,需要紧邻当前位置右侧的一个字母。
替换参数应该是 "\\\\'"
, 需要 4 个反斜杠才能替换为一个反斜杠。
请参阅Java demo :
String s= "//*[@title='ab'cd']";
System.out.println(s.replaceAll("(?<=\\p{L})'(?=\\p{L})", "\\\\'"));
关于java - 替换java中的撇号 ' in the middle of the word with\',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44726427/