我对下面的python有一些问题。我试图匹配单引号中的字符串,但只捕获内容,即删除单引号本身。
In [144]: tststr = "'hello'"
In [145]: res = re.search(r"'(.*)'", tststr)
In [146]: res.group()
Out[146]: "'hello'"我希望输出只包含"hello“,而不包含单引号。
谢谢你的帮忙!
发布于 2015-04-15 11:06:51
您需要指定组的组索引号,该组实际上存储捕获的字符。没有索引号,res.group()将打印所有匹配的字符,在您的情况下,它是'hello'。
res.group(1)例如:
>>> tststr = "'hello'"
>>> res = re.search(r"'(.*)'", tststr)
>>> res.group(1)
'hello'https://stackoverflow.com/questions/29648569
复制相似问题