python的Imrove Regex(2.7)寻找短 /不完整的英国邮政编码

发布于 2025-02-12 17:53:20 字数 545 浏览 4 评论 0原文

我有一些功能,可以在字符串中找到英国完整的邮政编码(例如DE2 7TT),并相应地返回它们。

但是,我想将其更改为还返回邮政编码,它到达一个或两个字母,然后是一个或两个数字(例如SE3,E2,SE45,E34)。

即,它必须收集两种形式的英国邮政编码(不完整和完整)。

代码是:

def pcsearch(postcode):
    if bool(re.search('(?i)[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', postcode)):
        postcode = re.search('(?i)[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', postcode)
        postcode = postcode.group()
        return postcode
    else:
        postcode = "na"
        return postcode

要使此调整还需要进行哪些调整,以便与这些较短,不完整的邮政编码一起使用?

I have a little function that finds full UK postcodes (e.g. DE2 7TT) in strings and returns them accordingly.

However, I'd like to change it to ALSO return postcodes it gets where there's either one or two letters and then one or two numbers (e.g. SE3, E2, SE45, E34).

i.e. it must collect BOTH forms of UK postcode (incomplete and complete).

The code is:

def pcsearch(postcode):
    if bool(re.search('(?i)[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', postcode)):
        postcode = re.search('(?i)[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', postcode)
        postcode = postcode.group()
        return postcode
    else:
        postcode = "na"
        return postcode

What tweaks are needed to get this to ALSO work with those shorter, incomplete, postcodes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2025-02-19 17:53:20

您可以使用交替和单词边界编写模式。

(?i)\b(?:[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}|[A-Z]{1,2}\d{1,2})\b

regex demo

只能通过检查匹配项即可使用该模式重新制定代码:

import re

def pcsearch(postcode):
       pattern = r"(?i)\b(?:[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}|[A-Z]{1,2}\d{1,2})\b"
       match = re.search(pattern, postcode)
       if match:
              return match.group()
       else:
              return  "na"

strings = [
       "SE3",
       "E2",
       "SE45",
       "E34",
       "DE2 7TT",
       "E123",
       "SE222"
]

for s in strings:
       print(pcsearch(s))

输出:输出:

SE3
E2
SE45
E34
DE2 7TT
na
na

You might write the pattern using an alternation and word boundaries.

(?i)\b(?:[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}|[A-Z]{1,2}\d{1,2})\b

Regex demo

The code could be refactored using the pattern only once by checking the match:

import re

def pcsearch(postcode):
       pattern = r"(?i)\b(?:[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}|[A-Z]{1,2}\d{1,2})\b"
       match = re.search(pattern, postcode)
       if match:
              return match.group()
       else:
              return  "na"

strings = [
       "SE3",
       "E2",
       "SE45",
       "E34",
       "DE2 7TT",
       "E123",
       "SE222"
]

for s in strings:
       print(pcsearch(s))

Output

SE3
E2
SE45
E34
DE2 7TT
na
na
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文