在\ w +中使用正则表达式搜索包含“ - ”。蟒蛇

时间:2013-06-07 20:00:55

标签: python regex search attributeerror

k = 'a bunch of data and then name ""Serpin-ps""'        
print re.search(r'name\s""(\w+)""',k).group(1)

给出:

AttributeError: 'NoneType' object has no attribute 'group'

desired_output = 'Serpin-ps'

有道理,因为文本中有' - '。

是否有正则表达式将' - '与所有其他字母数字字符合并?

1 个答案:

答案 0 :(得分:5)

您可以将预设字符类(如\w)放入显式字符类中。所以:

print re.search(r'name\s""([-\w]+)""',k).group(1)