Regex to get area and district codes from UK postcodes

Caballero

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

Spaces in poscodes are only there for clarity, they might or might not be there, therefor must be disregarded.

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

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: 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: 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: 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.

anubhava

You can use this regex:

([A-Z]+)([0-9]+).*?[A-Z0-9]{3}

and use matched group #1 and #2

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Regex to match the first half of a UK postcode

来自分类Dev

Scraping data from data.gov.uk / Regular Expression

来自分类Dev

Highmaps UK地图数据

来自分类Dev

How do I get the Area (or the path) of where a View is being scaffolded?

来自分类Dev

MVC5 Custom Route to Remove Controller Name from Area

来自分类Dev

Shortest match in regex from end

来自分类Dev

\ QWERTY UK键盘上的键

来自分类Dev

SQL Server UK日期损坏?

来自分类Dev

How to get regex match on my scenario

来自分类Dev

Get a certain value in a JSON string using a regex

来自分类Dev

`apt-get`和通配符:glob与regex

来自分类Dev

Get the word right before the coma with javascript and regex?

来自分类Dev

Php: regex get all numbers before colon

来自分类Dev

apt-get和通配符:glob与regex

来自分类Dev

regEx奇怪的“ apt-get purge”行为

来自分类Dev

使用newLine for Regex的file_get_contents()

来自分类Dev

Delete delimiter from columns csv regex

来自分类Dev

Ruby on Rails: Unable to retrieve regex from database

来自分类Dev

Count results from ElasticSearch with PHP regex

来自分类Dev

Regex match but exclude character from capture group

来自分类Dev

配置Azure Web和SQL UK文化

来自分类Dev

HTTP POST上的ServiceStack UK日期绑定

来自分类Dev

比较UK EndDate和UTC当前时间?

来自分类Dev

HTTP POST上的ServiceStack UK日期绑定

来自分类Dev

以 python 形式使用 uk-postcode-utils

来自分类Dev

视图 core.views.postcodes 没有返回 HttpResponse 对象。它返回 None 代替

来自分类Dev

使用name.co.uk或name-something.co.uk时的MySQL ORDER BY

来自分类Dev

正则表达式域不会获得域co.uk的.uk

来自分类Dev

explain these Linq Codes