如何使用re.findall()在python中为一个地址检查更多RegEx

时间:2018-12-27 15:34:14

标签: python regex

如何使用re.findall()在python中使用更多RegEx来检查一个地址

例如:我要应用以下正则表达式规则

 # need to get address
    txt = "hello user 44 West 22nd Street, New York, NY 12345 from"    
    regexp = "[0-9]{1,3} .+, .+, [0-9]{5}"
    regexp1 = "[0-9]{1,3} .+, .+, [A-Z]{2} [0-9]{5}"
    regexp2 = "[0-9]{1,3} .+, .+, [A-Z]{2} [0-9]{9}"    
    re.findall(regexp, regexp1, regexp2, txt)

这是正确的吗?我收到此代码错误

1 个答案:

答案 0 :(得分:1)

最后我从这里得到了答案

How to combine multiple regex into single one in python?

为此工作

import re
re1 = r'\d+\.\d*[L][-]\d*\s[A-Z]*[/]\d*'
re2 = '\d*[/]\d*[A-Z]*\d*\s[A-Z]*\d*[A-Z]*'
re3 = '[A-Z]*\d+[/]\d+[A-Z]\d+'
re4 = '\d+[/]\d+[A-Z]*\d+\s\d+[A-z]\s[A-Z]*'

sentences = [string1, string2, string3, string4]
generic_re = re.compile("(%s|%s|%s|%s)" % (re1, re2, re3, re4)).findall(sentence)