Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的正则表达式捕获组定义_Python_Regex - Fatal编程技术网

Python中的正则表达式捕获组定义

Python中的正则表达式捕获组定义,python,regex,Python,Regex,我对下面的python有一些问题。我试图匹配单引号内的字符串,但只捕获内容,即删除单引号本身 In [144]: tststr = "'hello'" In [145]: res = re.search(r"'(.*)'", tststr) In [146]: res.group() Out[146]: "'hello'" 我希望输出只包含“hello”,而不包含单引号 谢谢你的帮助 您需要指定实际存储捕获字符的组的组索引号。如果没有索引号,res.group()将打印您案例中的所有匹配字

我对下面的python有一些问题。我试图匹配单引号内的字符串,但只捕获内容,即删除单引号本身

In [144]: tststr = "'hello'"

In [145]: res = re.search(r"'(.*)'", tststr)

In [146]: res.group()
Out[146]: "'hello'"
我希望输出只包含“hello”,而不包含单引号


谢谢你的帮助

您需要指定实际存储捕获字符的组的组索引号。如果没有索引号,
res.group()
将打印您案例中的所有匹配字符,它是
'hello'

res.group(1)
例:


您需要指定实际存储捕获字符的组的组索引号。如果没有索引号,
res.group()
将打印您案例中的所有匹配字符,它是
'hello'

res.group(1)
例:


您需要指定实际存储捕获字符的组的组索引号。如果没有索引号,
res.group()
将打印您案例中的所有匹配字符,它是
'hello'

res.group(1)
例:


您需要指定实际存储捕获字符的组的组索引号。如果没有索引号,
res.group()
将打印您案例中的所有匹配字符,它是
'hello'

res.group(1)
例: