我有一个这样的清单
x=['[email protected]', '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']
我想提取这个元素 Our Studio is located at Bikini Berlin Terrace Level, 2nd floor Budapester Str. 46 10787 Berlin'
由于我正在为多个站点执行此操作,因此我想使用正则表达式来额外添加元素,以便它可以与其他站点一起使用。我想我可以通过说元素是否有小写和大写字母、数字、逗号,有时还有句点来获取元素。这是我尝试过的,但没有奏效。
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 = [
'[email protected]',
'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)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句