在java中用\'替换单词中间的撇号' [英] replace apostrophe ' in the middle of the word with \' in java

查看:40
本文介绍了在java中用\'替换单词中间的撇号'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XPath

I have an XPath

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

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

我想将它输出为

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

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

我正在使用此代码

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

但它正在输出

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

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

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

I couldn't find a similar question on StackOverflow.if there is please post the link in the comments.

推荐答案

要替换两个字母之间的 ',您需要一个 (?<=\p{L})'(?=\p{L}) 正则表达式.

To replace a ' in between two letters you need a (?<=\p{L})'(?=\p{L}) regex.

A (?<=\p{L}) 是正向后视,需要在当前位置的左侧紧接一个字母,而 (?=\p{L}) 是一个正向前瞻,需要一个字母紧跟当前位置的右侧.

A (?<=\p{L}) is a positive lookbehind that requires a letter immediately to the left of the current location, and (?=\p{L}) is a positive lookahead that requires a letter immediately to the right of the current location.

替换参数应该是"\\\\'",需要4个反斜杠才能用一个反斜杠替换.

The replacement argument should be "\\\\'", 4 backslashes are necessary to replace with a single backslash.

查看 Java 演示:

String s= "//*[@title='ab'cd']";
System.out.println(s.replaceAll("(?<=\\p{L})'(?=\\p{L})", "\\\\'"));

这篇关于在java中用\'替换单词中间的撇号'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆