Regex to match the first half of a UK postcode

MintDeparture

I need a regex to match the first half of a uk postcode, and it's possible just one half or a full post code could get passed in.

There is great way to match full postcode here: UK Postcode Regex (Comprehensive)

However I need to just get the first 3 or 4 characters of a postcode, with the possibility of half a post code or a postcode without spaces getting passed in, so any of these:

SL4 4BZ
SL44BZ - this will pull out just 'SL4'
SL4
DY10
DY10 3BD
DY10 3BD

The regex needs to match:

  • if it's 5 characters long (excluding spaces), remove last 3 characters and the first 2 remaining characters are the half-a-postcode
  • if it's 6 characters long (excluding spaces), remove last 3 characters and the first 3 remaining characters are the half-a-postcode
  • if it's 7 characters long (excluding spaces), remove last 3 characters and the first 4 remaining characters are the half-a-postcode
  • if it's 2, 3 or 4 characters long, keep it

But I can't quite get my head around how to write something this complex.

user557597

Updated for 2/5 characters as well, total coverage = 2/5 or 3/6 or 4/7

This works for the input samples.
Here are 2 variations of the same thing.

Results in Capture group 1

 #  (?m)^([A-Z0-9]{2,4})(?:\s*[A-Z0-9]{3})?$

 (?m)
 ^ 
 (                             # (1 start)
      [A-Z0-9]{2,4} 
 )                             # (1 end)
 (?: \s* [A-Z0-9]{3} )?
 $ 

Results in Capture group 0

 # (?m)^[A-Z0-9]{2,4}(?=(?:\s*[A-Z0-9]{3})?$)

 (?m)
 ^ 
 [A-Z0-9]{2,4} 
 (?=
      (?: \s* [A-Z0-9]{3} )?
      $ 
 )

@Smickie - Update - Regarding your question about inline-modifiers in JS.
(I'm not a JS expert but here is a simple example)
I don't believe inline modifiers construct are available. However, they can be added
via the flags parameter (enum) or in the /../flags notation.

You can see this example work in JSfiddle here --> http://jsfiddle.net/cdcv8uug/

var teststr = "SL\nSL 4BZ\nSL4BZ\nSL4\nSL4 4BZ\nSL44BZ\nDY10\nDY10 3BD\nDY10 3BD";

var arr = teststr.match( /^[A-Z0-9]{2,4}(?=(?:\s*[A-Z0-9]{3})?$)/mg );

var res = "Using match() -> Capture group 0\nteststr.match(/^[A-Z0-9]{2,4}(?=(?:\s*[A-Z0-9]{3})?$)/mg)\n";
for (var index = 0; index < arr.length; index++)
{
   res += index + ":  " +  arr[index] + "\n";
}

var re = /^([A-Z0-9]{2,4})(?:\s*[A-Z0-9]{3})?$/mg;
res += "\nUsing exec() -> Capture group 1\nvar re = /^([A-Z0-9]{2,4})(?:\s*[A-Z0-9]{3})?$/mg;\nre.exec(teststr)\n";

index = 0;
while ((arr = re.exec(teststr)) != null)
{
    res += index + ":  " + arr[1] + "\n";
    index++;
}

alert( res );

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

以 python 形式使用 uk-postcode-utils

来自分类Dev

Regex Exact Match - First and Last Name with One Space Between

来自分类Dev

Regex to get area and district codes from UK postcodes

来自分类Dev

Wrong RegEx match in Dart

来自分类Dev

Jquery Value match Regex

来自分类Dev

Regex multiple match substring

来自分类Dev

Search/Match regex in Python

来自分类Dev

为什么[regex] match()和-match不同?

来自分类Dev

为什么[regex] match()和-match不同?

来自分类Dev

Regex to match and limit character classes

来自分类Dev

Shortest match in regex from end

来自分类Dev

std :: regex_match与字符éèà

来自分类Dev

C ++ regex_match行为

来自分类Dev

Codeigniter regex_match与比较

来自分类Dev

使用Regex的jQuery First Upper

来自分类Dev

查找Regex.Match失败的地方

来自分类Dev

C ++无法使regex_match正常工作

来自分类Dev

PHP: Trying to understand subpattern regex match

来自分类Dev

How to dynamically create regex to use in .match Javascript?

来自分类Dev

Using regex in Scala to group and pattern match

来自分类Dev

How to get regex match on my scenario

来自分类Dev

Javascript regex match()返回匹配的部分(子集)

来自分类Dev

C ++ boost :: regex_match奇怪的行为

来自分类Dev

Laravel Regex Match Url with JPG not working

来自分类Dev

Regex: How to match sequence of SAME characters?

来自分类Dev

Regex match but exclude character from capture group

来自分类Dev

Java Regex: How to Match URL Path?

来自分类Dev

需要preg_match / regex格式

来自分类Dev

Regex - Match all numbers until character