Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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,我有一段代码试图从字符串中提取一个数值,在本例中为'rel',如下所示: str1 = 'span class="stars" rel="3.2" style="width:64.0%" title="Rating 3.2 out of 5 stars">/span>' match = re.findall(r'span rel="(\d+)*(\.\d+)".\*>/span>', str1) if match: print("it's here!")

我有一段代码试图从字符串中提取一个数值,在本例中为'rel',如下所示:

str1 = 'span class="stars" rel="3.2" style="width:64.0%" title="Rating 3.2 out of 5 stars">/span>'

match = re.findall(r'span rel="(\d+)*(\.\d+)".\*>/span>', str1)

if match:
    print("it's here!")          
else:
    print("not found")
但是,它不起作用。有人知道我需要做哪些调整吗


提前感谢,

您可能正在寻找此

str1 = 'span class="stars" rel="3.2" style="width:64.0%" title="Rating 3.2 out of 5 stars">/span>'

match = re.findall(r'span.*?rel="(\d+)(\.\d+)".*>/span>', str1)

print (match)

您找不到任何东西的主要原因是,您的正则表达式需要在span之后立即具有
rel
,而您的
str1
没有遵循。在span和rel之间添加一个
*?
将不会贪婪地捕获其他所有内容

你好,刘,对不起,我花了这么长时间才回复你。您的解决方案确实有效,但我发现doing match=re.findall(r'[