在 Python 中提取撇号之间的引号

Extracting quotes between apostrophes in Python

我正在研究正则表达式以捕获引号中的文本。它有效,但是作为源文件的纯文本已将单个智能引号转换为撇号。

对于 regex 我有:

r("[\"|\'|\`].+[\"|\'|\`]")

正则表达式工作正常,但也可以抓取两个撇号之间的文本。是否可以调整正则表达式使其不这样做?

"Come up and see me some time" # correct
'Yeah, I wonder if will pick this up to' #correct
`Mmmm. I wonder...` # correct
"Sorry about the mess!" #correct
We don't know who is human. Don't we? # Wrong. 

最后一个抢

't know who is human. Don'

我也会推荐非单词边界(\B),就像@Wiktor 评论的那样,但也使用反向引用(</code>)来匹配与起始引号字符相同的引号字符:</p> <pre><code>regex = r"\B([\"'`]).+?\B"

在这里测试 https://regex101.com/r/TOLYVc/3