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
Regex 使用正则表达式确保某些内容包含单独的数字_Regex_Python 3.x - Fatal编程技术网

Regex 使用正则表达式确保某些内容包含单独的数字

Regex 使用正则表达式确保某些内容包含单独的数字,regex,python-3.x,Regex,Python 3.x,我试图在网站上搜索公司的位置。我有这个功能: x=['174 WEST 4TH ST, NYC','All contents © Copyright 2018 Propela'] import re def is_location(text): """Does text contain digits, lowercase and uppercase letters""" return all(re.search(pattern, text) for pattern in ['

我试图在网站上搜索公司的位置。我有这个功能:

x=['174 WEST 4TH ST, NYC','All contents © Copyright 2018 Propela']

import re

def is_location(text):
    """Does text contain digits, lowercase and uppercase letters"""
    return all(re.search(pattern, text) for pattern in ['\d{3,16}', '[a-z]*', '[A-Z]'])
# x[1]
# is_location(x[2])

print(list(filter(is_location, x)))
我想使用正则表达式,只在两次提到数字的情况下才能捕捉到东西,所以在纽约市西四街174号,有一组数字,然后是另一个独立的数字


这可能吗

您可以使用以下模式匹配字符串中出现在不同单词中的两个数字:

\d+.*\s+.*\d+
下面是一个示例代码:

line = "174 WEST 4TH ST, NYC";

res = re.search( r'\d+.*\s+.*\d+', line, re.M|re.I)
if res:
    print "found a match: ", res.group()
else:
    print "no match"

什么意思?我在其中放了一个示例What is re.M | re.IIt启用边界标记,并使搜索不区分大小写。如果需要,您可以添加/删除它们。我可以将r'\d+.*\s+.*\d+'添加到我的函数中,然后去掉我已有的数字吗?@Bob我不明白。你想干什么?