匹配python的单引号

Raj*_*eev 3 python

如何匹配以下我希望所有名称与单引号

This hasn't been much that much of a twist and turn's to 'Tom','Harry' and u know who..yes its 'rock'
Run Code Online (Sandbox Code Playgroud)

如何仅在单引号内提取名称

name = re.compile(r'^\'+\w+\'')
Run Code Online (Sandbox Code Playgroud)

NPE*_*NPE 7

以下正则表达式查找括在引号中的所有单个单词:

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

这里:

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

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

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