引号内的撇号模式 [英] Pattern for apostrophe inside quotes

查看:57
本文介绍了引号内的撇号模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种可以找到单引号内的撇号的模式.例如文本

I am looking for a pattern that can find apostrophes that are inside single quotes. For example the text

Foo '不能'禁止'不要'

Foo 'can't' bar 'don't'

我想找到并替换 can't 和 don't 中的撇号,但我不想找到单引号

I want to find and replace the apostrophe in can't and don't, but I don't want to find the single quotes

我尝试过类似的东西

(.*)'(.*)'(.*)'

并在第二个匹配组上应用替换.但是对于包含 2 个带撇号的单词的文本,这种模式不起作用.

and apply the replace on the second matching group. But for text that has 2 words with apostrophes this pattern won't work.

为了澄清文本可以有单引号,其中没有撇号,应按原样保留.例如

to clarify the text could have single quotes with no apostrophes inside them, which should be preserved as is. For example

'foo' '不能' 禁止'不要'

'foo' 'can't' bar 'don't'

我仍然只寻找撇号,所以 foo 周围的单引号不应该匹配

I am still looking for only apostrophes, so the single quotes around foo should not match

推荐答案

我相信你需要要求word"字符出现在 ' 符号之前和之后,这可以用一个词边界:

I believe you need to require "word" characters to appear before and after a ' symbol, and it can be done with a word boundary:

\b'\b

查看正则表达式演示

要只匹配字母中的引用,请使用

To only match the quote inside letters use

(?<=\p{L})'(?=\p{L})
(?<=[[:alpha:]])'(?=[[:alpha:]])
(?U)(?<=\p{Alpha})'(?=\p{Alpha})  # Java, double the backslashes in the string literal

或仅 ASCII

(?<=[a-zA-Z])'(?=[a-zA-Z])

这篇关于引号内的撇号模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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