Do Not Sell My Personal Information Jump to content


  • Join The Club

    Join the Lexus Owners Club and be part of the Community. It's FREE!

     

Regular Expressions


lelandv
 Share

Recommended Posts

Is someone able to sanity-check this regex for me?

It's designed to verify the format of a UK post code (in general formatting terms) - it does not do extended validation (i.e. if the first element has a letter suffix, it must have one or two letter prefixes and only one number etc.) but I think it should validate the general format.

^([a-z]|[A-Z]){1,2}(\d){1,2}([a-z]|[A-Z])?(\s)*(\d){1}([a-z]|[A-Z]){2}

The intended logic is:

Must begin with at least one letter but no more than 2, followed by one or two digits, followed by 0 or 1 letter;

may then have whitespace or none at all, followed by:

1 digit, and 2 letters.

Thanks.

Leland

Link to comment
Share on other sites

Actually.. thinking about it.. could catch the extended format validation perhaps?

^([a-z]|[A-Z]){1,2}(\d){1}(\w)?(\s)*(\d){1}([a-z]|[A-Z]){2}

Feasible, no?

L.

the issue is, there can be 1 or 2 letters, before 1 or 2 numbers, followed by 2 letters..

hmm.. you mean in the first element?

I know you can have, for example:

WC1A

MK43

N18

W1

but I don't thinkyou can have:

W1AC

MK43B

W18A

and as far as I'm aware the last element is always a fixed format of one digit and two letters..

such as:

MK43 8ZZ

or

WC1A 3BQ

never seen anything different for the second element... or am I misguided?

Leland

Link to comment
Share on other sites

Thanks...

My final version is this:

^([a-zA-Z]){1,2}(\d){1}(\w)?(\s)*(\d){1}([a-zA-Z]){2}

which works in Perl, PHP, .NET (C#, J#, VB, C++), but I have a problem with ColdFusion...

Specifically the function:

REFind("^([a-zA-Z]){1,2}(\d){1}(\w)?(\s)*(\d){1}([a-zA-Z]){2}","WC1A 3LL")

Returns 0, meaning that it did not match at all. All the other languages match it correctly, and also pick up if an invalid format is used. ColdFusion, however, doesn't seem to want to play ball... though it accepts the syntax itself as "valid" without generating any errors...

Anyone have any ideas for coldfusion?

Thanks,

Leland

Link to comment
Share on other sites

Does Cold Fusion accept WC1A3LL (i.e. without the space)?

If so, it's a case of stripping all spaces, then parsing the postcode, then some sort of character shift along the lines of the following (written in Pascal in this case):-

postcode:=postcode+" "; {adds a space to the end, increasing length by 1}

postcode[length(postcode)]:=postcode[length(postcode)-1];

postcode[length(postcode)-1]:=postcode[length(postcode)-2];

postcode[length(postcode)-2]:=postcode[length(postcode)-3];

The last three lines nudge the last three characters along to recreate the gap.

Link to comment
Share on other sites


Does Cold Fusion accept WC1A3LL (i.e. without the space)?

If so, it's a case of stripping all spaces, then parsing the postcode, then some sort of character shift along the lines of the following (written in Pascal in this case):-

postcode:=postcode+" "; {adds a space to the end, increasing length by 1}

postcode[length(postcode)]:=postcode[length(postcode)-1];

postcode[length(postcode)-1]:=postcode[length(postcode)-2];

postcode[length(postcode)-2]:=postcode[length(postcode)-3];

The last three lines nudge the last three characters along to recreate the gap.

Nope.. CF doesn't accept any format with or without spaces... I suspect that there's something funny about CF's implementation of Regex :duh:

L.

Link to comment
Share on other sites

Latest Deals

Lexus Official Store for genuine Lexus parts & accessories

Disclaimer: As the club is an eBay Partner, The club may be compensated if you make a purchase via eBay links

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share






Lexus Owners Club Powered by Invision Community


eBay Disclosure: As the club is an eBay Partner, the club may earn commision if you make a purchase via the clubs eBay links.

DISCLAIMER: Lexusownersclub.co.uk is an independent Lexus forum for owners of Lexus vehicles. The club is not part of Lexus UK nor affiliated with or endorsed by Lexus UK in any way. The material contained in the forums is submitted by the general public and is NOT endorsed by Lexus Owners Club, ACI LTD, Lexus UK or Toyota Motor Corporation. The official Lexus website can be found at http://www.lexus.co.uk
×
  • Create New...