地址的正则表达式在正则表达式 101 (Python) 中工作,而不是在 Python 中使用 re.match?

Regex for Address working in Regex 101 (Python) not in Python using re.match?

我有以下 Python 脚本(在 Jupyter 中),它应该使用正则表达式提取地址信息(在此步骤之前已经清理了单元号并且缩写了街道类型):

type_opts = r"Terrace|Way|Walk|St|Rd|Ave|Cl|Ct|Cres|Blvd|Dr|Ln|Pl|Sq|Pde"
road_attrs_pattern = r"(?P<rd_no>\w?\d+(\-\d+)?\w?\s+)(?P<rd_nm>[a-zA-z \-]+)(?#\s+(?P<rd_tp>" + type_opts + ")"
print("Road Attr Pattern: ", road_attrs_pattern)
road_attrs = re.match(road_attrs_pattern, proc_addr)
road_num = road_attrs.group('rd_no').strip()
print("Road number: ", road_num)
road_name = road_attrs.group('rd_nm').strip()
print("Road name: ", road_name)
road_type = road_attrs.group('rd_tp').strip()
print("Road type: ", road_type)

我正在使用这个地址:

Burrah lodge, 15 Anne Jameson Pl

结果如下:

Road Attr Pattern:  (?P<rd_no>\w?\d+(\-\d+)?\w?\s+)(?P<rd_nm>[a-zA-z \-]+)(?#\s+(?P<rd_tp>Terrace|Way|Walk|St|Rd|Ave|Cl|Ct|Cres|Blvd|Dr|Ln|Pl|Sq|Pde)

但随后抛出错误,指出街道号码不可用 AttributeError: 'NoneType' object has no attribute 'group'

但是 Regex101 中的复制粘贴 here 说它应该工作,并且查看 Regex 我认为它也应该工作...

它应该打印出以下内容:

Road Attr Pattern:  (?P<rd_no>\w?\d+(\-\d+)?\w?\s+)(?P<rd_nm>[a-zA-z \-]+)(?#\s+(?P<rd_tp>Terrace|Way|Walk|St|Rd|Ave|Cl|Ct|Cres|Blvd|Dr|Ln|Pl|Sq|Pde)
Road number: 15
Road name: Anne Jameson
Road type: Pl

根据 the docsre.match 检查字符串 开头的匹配项

由于您要查找从字符串中途开始的匹配项,因此您需要 re.search