Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Expression - Fatal编程技术网

在字符串列表(Python)的每一项中查找特定的模式(正则表达式)

在字符串列表(Python)的每一项中查找特定的模式(正则表达式),python,list,expression,Python,List,Expression,从这个列表problems=[“32+698”,“3801-2”,“45+43”,“123+49”] 我想得到这些数字“\d*\s”,意思是32380145123 有没有一种方法可以查看列表中的每一项,并用列表进行搜索 我只有这一次尝试打印([x代表x如果重新搜索(r'\d*\s',x)],但是如果“\d*\s”在那里,它会打印整个项目,所以我再次得到[“32+698”,“3801-2”,“45+43”,“123+49”] (我的目标是使每个项目的长度更长,即3,4,2,3)您需要在字符串中的空

从这个列表
problems=[“32+698”,“3801-2”,“45+43”,“123+49”]

我想得到这些数字
“\d*\s”
,意思是32380145123

有没有一种方法可以查看列表中的每一项,并用列表进行搜索

我只有这一次尝试
打印([x代表x如果重新搜索(r'\d*\s',x)]
,但是如果
“\d*\s”
在那里,它会打印整个项目,所以我再次得到
[“32+698”,“3801-2”,“45+43”,“123+49”]


(我的目标是使每个项目的长度更长,即3,4,2,3)

您需要在字符串中的空格之前提取第一部分。这可以使用组捕获来完成。以下是您可以使用的代码:

[re.search('(\d+)\s', x).group(1) for x in problems if re.search(r'(\d)*\s',x)]
这里,
(\d+)\s
匹配空白字符前的一个或多个数字,我们使用
组(1)
方法提取第一个组