正则表达式从英国邮政编码获取区号和区号 [英] Regex to get area and district codes from UK postcodes
问题描述
基本上,我需要一个正则表达式来匹配英国邮政编码中的第一个字母序列(简单)和另一个匹配以下数字的正则表达式(不太容易).
Basically, I need a regex that would match first sequence of letters in UK postcode (easy) and another regex which would match the following number (not so easy).
可能的组合和匹配示例:
Examples of possible combinations and matches:
AA9A 9AA regex1: AA regex2: 9
A9A 9AA regex1: A regex2: 9
A9 9AA regex1: A regex2: 9
A99 9AA regex1: A regex2: 99
AA9 9AA regex1: AA regex2: 9
AA99 9AA regex1: AA regex2: 99
poscodes 中的空格只是为了清楚起见,它们可能存在也可能不存在,因此必须忽略.
Spaces in poscodes are only there for clarity, they might or might not be there, therefor must be disregarded.
我可以使用这两个正则表达式的一些帮助,尤其是 regex2(匹配数字).顺便说一句,我将在 MySQL 5.6 中运行它
I could use some help with these two regular expressions, especially regex2 (matching the number). By the way, I'm gonna be running this in MySQL 5.6
更新:请注意:无论如何,最后 3 个字符将始终被忽略,这是唯一始终适用的模式 - 从我的示例中应该可以看到.
UPDATE: Just to note: last 3 characters will always be ignored no matter what, that's the only pattern that always applies - it should be visible from my examples.
UPDATE2:对不起,如果我之前不够清楚 - 我不需要验证邮政编码,我需要提取区号和区号.
UPDATE2: Sorry if I wasn't clear enough before - I don't need to validate the post codes, I need to extract area and district codes.
UPDATE3: 再次强调:必须忽略空格,它可以是 AA99 9AA
或 AA999AA
- 在这两种情况下都是 regex2(提取号)应该是 99
UPDATE3: Just to emphasize again: white space must be disregarded, it could be AA99 9AA
or AA999AA
- in both cases the regex2 (extract number) should be 99
UPDATE4:感谢您的所有回答,不幸的是我刚刚发现 MySQL 无法提取正则表达式匹配项.一路走好,MySQL,再次证明是无用的.
UPDATE4: Thanks for all your answers, unfortunately I just found out that MySQL can't extract regex matches. Way to go, MySQL, proving to be useless yet again.
推荐答案
你可以使用这个正则表达式:
You can use this regex:
([A-Z]+)([0-9]+).*?[A-Z0-9]{3}
并使用匹配组#1
和#2
这篇关于正则表达式从英国邮政编码获取区号和区号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!