gpt4 book ai didi

java - 在java中替换撇号 ' in the middle of the word with\'

转载 作者:行者123 更新时间:2023-11-30 07:53:28 24 4
gpt4 key购买 nike

我有一个 XPath

//*[@title='ab'cd']

我想把它输出为

//*[@title='ab\'cd']

我正在使用这段代码

property = property.replaceAll("^[a-zA-Z]+(?:'[a-zA-Z]+)*", "\'");

但是它正在输出

//*[@text='ab'cd']

我在 StackOverflow 上找不到类似的问题。如果有请在评论中发布链接。

最佳答案

替换'在两个字母之间你需要一个 (?<=\p{L})'(?=\p{L})正则表达式。

A (?<=\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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com