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'
有道理,因为文本中有' - '。
是否有正则表达式将' - '与所有其他字母数字字符合并?
答案 0 :(得分:5)
您可以将预设字符类(如\w
)放入显式字符类中。所以:
print re.search(r'name\s""([-\w]+)""',k).group(1)