Regex用于查找State和Zip from地址

Regex用于查找State和Zip from地址,regex,Regex,正在尝试生成可以从地址获取状态的正则表达式 德克萨斯州休斯顿贝拉尔大道1-1234号123室,邮编77036 德克萨斯州休斯顿市贝莱尔街123号2-1234号,邮编77036 我有这个给州政府的 \w{2}(?=\s\d{1,5}) 这是给Zip的吗 (?我不认为正则表达式是实现这一点的最佳方法。相反,我会使用a将地址解析为其组件。您需要state_缩写并进行排序。响应示例: [ { "input_index": 0, "candidate_index":

正在尝试生成可以从地址获取状态的正则表达式

德克萨斯州休斯顿贝拉尔大道1-1234号123室,邮编77036

德克萨斯州休斯顿市贝莱尔街123号2-1234号,邮编77036

我有这个给州政府的

\w{2}(?=\s\d{1,5})

这是给Zip的吗


(?我不认为正则表达式是实现这一点的最佳方法。相反,我会使用a将地址解析为其组件。您需要state_缩写并进行排序。响应示例:

[
    {
        "input_index": 0,
        "candidate_index": 0,
        "delivery_line_1": "1 Santa Claus Ln",
        "last_line": "North Pole AK 99705-9901",
        "delivery_point_barcode": "997059901010",
        "components": {
            "primary_number": "1",
            "street_name": "Santa Claus",
            "street_suffix": "Ln",
            "city_name": "North Pole",
            "state_abbreviation": "AK",
            "zipcode": "99705",
            "plus4_code": "9901",
            "delivery_point": "01",
            "delivery_point_check_digit": "0"
        },
        "metadata": {
            "record_type": "S",
            "zip_type": "Standard",
            "county_fips": "02090",
            "county_name": "Fairbanks North Star",
            "carrier_route": "C004",
            "congressional_district": "AL",
            "rdi": "Commercial",
            "elot_sequence": "0001",
            "elot_sort": "A",
            "latitude": 64.75233,
            "longitude": -147.35297,
            "precision": "Zip8",
            "time_zone": "Alaska",
            "utc_offset": -9,
            "dst": true
        },
        "analysis": {
            "dpv_match_code": "Y",
            "dpv_footnotes": "AABB",
            "dpv_cmra": "N",
            "dpv_vacant": "N",
            "active": "Y",
            "footnotes": "L#"
        }
    },

    {
        "input_index": 1,
        "candidate_index": 0,
        "addressee": "Apple Inc",
        "delivery_line_1": "1 Infinite Loop",
        // truncated for brevity
    }
]
希望有帮助。

您可以与“,([A-Z]{2})”匹配,然后状态将是由括号匹配的子模式

import re

s1 = "1- 1234 Bellaire Blvd, Suite 123, Houston, TX 77036"

s2 = "2- 1234 BELLAIRE BL #123, HOUSTON, TX 77036"

m = re.search(', ([A-Z]{2}) ', s1)

print(m.group(1))