Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
Regex 从正则表达式中的字符串而不是年份部分提取数字_Regex - Fatal编程技术网

Regex 从正则表达式中的字符串而不是年份部分提取数字

Regex 从正则表达式中的字符串而不是年份部分提取数字,regex,Regex,我有下面的字符串,我想提取数字,但不是日期-即2020年2月15日或2020年2月19日 请注意,数字可以用、或\r\n或空白或任何其他字符分隔 test_string = 'Get me data for the below \r\n11011\r\n11045\r\n11051\r\n11063\r\n11079 12462,13003 starting 2/15/2020 to 2/19/2020' 我用过: re.compile("(\d+)[\r\n*|\n\r*|\,.

我有下面的字符串,我想提取数字,但不是日期-即2020年2月15日或2020年2月19日

请注意,数字可以用、或\r\n或空白或任何其他字符分隔

test_string = 'Get me data for the below \r\n11011\r\n11045\r\n11051\r\n11063\r\n11079 12462,13003  starting 2/15/2020 to 2/19/2020'
我用过:

re.compile("(\d+)[\r\n*|\n\r*|\,.|\r*|\s*]").findall(s)
预计产量:

['11011', '11045', '11051', '11063', '11079', '12462', '13003']
但我得到了下面的答案

['11011', '11045', '11051', '11063', '11079', '12462', '13003', '2020']
代码

import re
test = 'Get me data for the below \r\n11011\r\n11045\r\n11051\r\n11063\r\n11079 12462,13003  starting 2/15/2020 to 2/19/2020'
re.findall('(\d{5,})'), test)
输出:

['11011', '11045', '11051', '11063', '11079', '12462', '13003']
解释

它匹配长度>=5的每个数字

代码

import re
test = 'Get me data for the below \r\n11011\r\n11045\r\n11051\r\n11063\r\n11079 12462,13003  starting 2/15/2020 to 2/19/2020'
re.findall('(\d{5,})'), test)
输出:

['11011', '11045', '11051', '11063', '11079', '12462', '13003']
解释


它匹配长度>=5的每个数字

[\r\n*\n\r*\s*.\r*\s*]
是错误的模式,您必须使用
(?:…)
是。最初我只是使用了re.compile(“(\d+)).findall(s),但我得到了包括月份和日期在内的所有数字。然后我就开始了?:但这一次我得到了上面的年份。好的,一个快速的解决方案是匹配你不需要的,匹配并捕获你需要的<代码>[x代表x在re.findall(r')(?这对我来说很有用。
[\r\n*\r*\r*\s*,.\r*\r*\s*]
是一个错误的模式,你一定是想使用
(?:…)
是的。我刚才用的是re.compile((\d+)。findall(s),但我得到了包括月份和日期在内的所有数字。然后我就开始了?:但这次我得到了以上年份的数字。好的,一个快速的解决方案是匹配您不需要的数据,并匹配和捕获您需要的数据。
[x代表x在re.findall(r'(?这对我很有用。你能告诉我如果数字长度是4,我可能会有任何长度的数字吗?如果你将长度设置为4,它也会捕获2020。现在我正在寻找一个更全局的变体,这非常具体到你的情况。你能告诉我如果数字长度是4,我可能会有任何长度的数字吗?如果你将长度设置为4,它将被捕获ch 2020现在我正在寻找一种更具全球性的变体,这是非常具体的