【发布时间】:2018-07-02 01:13:24
【问题描述】:
我正在使用正则表达式来捕获引号中的文本。它可以工作,但是作为源文件的纯文本已将单引号转换为撇号。
对于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["'`].+?["'`]\B -
在引号中加上转义斜线,这就是答案,谢谢:)
-
您不需要转义它们,只需使用
r"""pattern_here"""。 -
我在 regex101 中遇到错误,告诉我要逃避它们,仅此而已。
-
只要学会使用regex101 - regex101.com/r/TOLYVc/4
标签: regex python-2.7 text quotes