Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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=['hello@thepowerhouse.group', 'ThePowerHouse\xa0 is a part of the House of ElektroCouture', 'Our Studio is located at Bikini Berlin Terrace Level, 2nd floor Budapester Str. 46 10787 Berlin', '\xa0', 'Office:\xa0+49 30 20837551', '\xa0', '

我有一份这样的清单

x=['hello@thepowerhouse.group', 'ThePowerHouse\xa0 is a part of the House of ElektroCouture', 'Our Studio is located at Bikini Berlin Terrace Level, 2nd floor Budapester Str. 46 10787 Berlin', '\xa0', 'Office:\xa0+49 30 20837551', '\xa0', '\xa0']
我想提取这个元素,我们的工作室位于比基尼柏林露台层,布达佩斯大街46 10787号二楼,柏林

因为我为几个站点做这项工作,所以我想用正则表达式来增加元素,这样它就可以与其他站点一起工作。我想我可以这样理解元素:如果元素有小写和大写字母、数字、逗号,有时还有句点。这是我尝试过的,但没有成功

import re
for element in x:
    if re.findall("([A-Za-z0-9,])",element)==True:
        print("match")

您可以将您的规则拆分为几个简单的正则表达式,并按顺序测试它们,而不是生成一些怪物表达式

import re

def is_location(text):
    """Returns True if text contains digits, uppercase and lowercase characters."""
    patterns = r'[0-9]', r'[a-z]', r'[A-Z]'
    return all(re.search(pattern, text) for pattern in patterns)

x = [
    'hello@thepowerhouse.group',
    'ThePowerHouse\xa0 is a part of the House of ElektroCouture',
    'Our Studio is located at Bikini Berlin Terrace Level, 2nd floor Budapester Str. 46 10787 Berlin',
    '\xa0', 'Office:\xa0+49 30 20837551', '\xa0', '\xa0'
]

print(next(filter(is_location, x)))

不是要走的路。不能依赖元素内容。而且,实际上,您还需要解析一个分隔元素。您的意思是什么?哦,您已经有了这些元素。但是,您不能依靠内容字符来获取地址。要验证元素是否具有所有/大部分这些字符,必须使用单独的断言?=.*[A-Z]?=.*[A-Z]?=.*[A-Z]?=.*\d?=.*,等等。接下来做什么?这是一个python内置函数。它只是从过滤器返回的迭代器对象中提取第一个条目。接下来是引擎盖下用于循环的内容。这在这里不是必要的。我明白了,如果我想要所有的答案呢?那么你可以使用listfilteris_位置,例如x。您也可以使用列表理解:[a代表x中的a,如果是位置a]