我将如何修改这个正则表达式来提取的英国邮政code的左侧和右侧部分? [英] How would I modify this regex to extract the left and right hand parts of a UK postal code?

查看:111
本文介绍了我将如何修改这个正则表达式来提取的英国邮政code的左侧和右侧部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正规的前pression其中工程验证英国邮政codeS,但现在我想提取的code中的组成部分,我感到困惑。对于那些谁不知道英国邮政codeS的例子是WC1 1AA,WC11 1AA和M1 1AA。

下面的(道歉格式)定期EX pression处理缺乏空间(这是 \ S {0,} 位)之间的左右两部分,仍然验证(这是伟大的)。

<$p$p><$c$c>(?:(?:A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d(?:\d|[A-Z])?\s{0,}\d[A-Z]{2})

我希望现在能够提取的左手和右手边,我知道,支架用于此,但目前已经有括号存在的,正则表达式规范是不容易阅读。所以我想这些支架需要更换,谁能帮助我返工我的支架?

我可以看到其他人会觉得这个表达式使用,所以请随时用它来验证英国的邮政地址。

解决方案

其实,括号是用来提取,没有括号

。在(?:构建在你的前pression你是如何的 prevent 的括号内无法执行提取你会想:

<$p$p><$c$c>(?:((?:A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d(?:\d|[A-Z])?)\s{0,}(\d[A-Z]{2}))

顺便说一句,我也将促使这一变化:

<$p$p><$c$c>(?:((?:A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d(?:\d|[A-Z])?)\s*(\d[A-Z]{2}))

由于 \ S {0,} 是一种愚蠢的方式来写的 \ S *

I have a regular expression which works for validating UK postal codes but now I would like to extract the constituent parts of the code and I'm getting confused. For those who do not know examples of UK postal codes are 'WC1 1AA', 'WC11 1AA' and 'M1 1AA'.

The regular expression below (apologies for the formatting) handles the lack of a space (this is the \s{0,} bit) between the left and right parts and still validates (which is great).

(?:(?:A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d(?:\d|[A-Z])?\s{0,}\d[A-Z]{2})

I'd like to be able to extract the left and right hand sides now and I know that brackets are used for this, but there are already brackets in there and the regex specification is not easy to read. So I guess these brackets need replacing, can anyone help me rework my brackets?

I can see other people would find this regex of use, so please feel free to use it for validating UK postal addresses.

解决方案

Actually, parentheses are used for extraction, not brackets. The (?: constructs in your expression are how you prevent parentheses from performing extraction. You would want:

(?:((?:A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d(?:\d|[A-Z])?)\s{0,}(\d[A-Z]{2}))

Incidentally, I would also make this change:

(?:((?:A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d(?:\d|[A-Z])?)\s*(\d[A-Z]{2}))

because \s{0,} is a goofy way to write \s*.

这篇关于我将如何修改这个正则表达式来提取的英国邮政code的左侧和右侧部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆