Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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_Python 3.x_Regex - Fatal编程技术网

正则表达式数组python

正则表达式数组python,python,python-3.x,regex,Python,Python 3.x,Regex,我知道有很多关于regex的信息,但我不知怎么搞不懂 我有一个array1=['\n 1.979\n\n 1.799\n']看起来像这样,但数字不同,但总是以这种格式,因此regex=re.compiler'\d.\d\d\d'在notepad++中非常匹配,但在python中似乎不起作用 import re regex = re.compile(r'\d.\d\d\d') filteredarray= [i for i in array1 if regex.match(i)] print(

我知道有很多关于regex的信息,但我不知怎么搞不懂

我有一个array1=['\n 1.979\n\n 1.799\n']看起来像这样,但数字不同,但总是以这种格式,因此regex=re.compiler'\d.\d\d\d'在notepad++中非常匹配,但在python中似乎不起作用

import re 
regex = re.compile(r'\d.\d\d\d')
filteredarray= [i for i in array1 if regex.match(i)]

print(filteredarray)
我错过了什么? 提前感谢

您可以使用re.findallexpression,string查找所需的值并将其转换为列表

您的需求的正确正则表达式是\d\。?\d{3},或者您也可以使用\d\。\d\d

我认为您的模式\d。\d\d\d不在\n 1.979\n\n 1.799\n的范围内。您只需将\d.\d\d\d替换为^[\s\s]+\d.\d\d

详情:

^:字符串的开头 [\s\s]+:匹配任何字符,包括换行符。 我还尝试了python上的测试结果

import re
array1 = ['\n 1.979   \n, \n 1.799   \n']
regex = re.compile(r'^[\s\S]+\d.\d\d\d')

filteredarray= [i for i in array1 if regex.match(i)]

print(filteredarray)
结果

['\n 1.979   \n, \n 1.799   \n']

这是长度为1的列表中的单个字符串。数字不是您发布的格式,因为字符串中有空格和换行符。您确定记事本++处于正则表达式模式而非扩展模式吗?请再次阅读文档以进行匹配。改用。若要创建数字字符串数组,请执行以下操作:Regex.findallarray1[0]@Thefourthbird TypeError:search缺少1个必需的位置参数:“string”谢谢,但为什么我的正则表达式错误?它在正则表达式101上也完全匹配。您的也正确,但是[i for i in array1 if regex.matchi]这部分错误。您正在数组中指定表示“i”的值。当您在array1中指定相同的值时,您将在array1中获得相同的值array@Dinesh听起来你已经忘记了为什么要逃逸他们的点。我没有逃逸字符串中的点。是的,你在正则表达式字符串中逃逸了。
['\n 1.979   \n, \n 1.799   \n']