python - 匹配来自 python re 的单引号

标签 python

如何匹配下面我想要的所有名字都用单引号

This hasn't been much that much of a twist and turn's to 'Tom','Harry' and u know who..yes its 'rock'

如何只提取单引号内的名字

name = re.compile(r'^\'+\w+\'')

最佳答案

以下正则表达式查找所有用引号引起来的单词:

In [6]: re.findall(r"'(\w+)'", s)
Out[6]: ['Tom', 'Harry', 'rock']

这里:

  • '匹配单引号;
  • \w+匹配一个或多个单词字符;
  • '匹配单引号;
  • 圆括号组成一个捕获组:它们定义了 findall() 返回的匹配部分。

如果您只想查找以大写字母开头的单词,可以像这样修改正则表达式:

In [7]: re.findall(r"'([A-Z]\w*)'", s)
Out[7]: ['Tom', 'Harry']

关于python - 匹配来自 python re 的单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9991933/

相关文章:

python - 使用 scikit learn 检索错误分类的文档

python - 无法让 python-can-viewer 运行

python - wxpython 的 Intellisense 之类的小部件是什么

Python 默认字典给出重复键

python - 当链接将重定向到另一个时如何通过 urllib 获取内容?

python - .read() 在 python 中使用askopenfilename() 返回 "unicode object has no attribute read"

python - 使用turtle.onscreenclick查找鼠标单击的坐标

python - 使用 Pandas 查找自滚动高点以来的周期数

python - PyQt:Dialog 的最小化窗口按钮在 OSX 中丢失

python - 正则表达式查找附近位置多次出现相同字符串的单词