Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: UK postcode regex

by Gilimanjaro (Hermit)
on Apr 21, 2005 at 13:55 UTC ( #449987=note: print w/replies, xml ) Need Help??


in reply to UK postcode regex

#!/usr/bin/perl -l $zipadidoda = 'le12 ogx'; print "In: $zipadidoda"; if( $zipadidoda =~ s/^([A-Z]{1,2}\d{1,2}[A-Z]?)\s?(O|\d)([A-Z]{2})$/uc +("$1 ".(0+$2).$3)/ie ) { print "Out: $zipadidoda"; } else { print "Invalid zipcode"; }

You wanted a single re, so you got one!

Or is the 'e' modified considered cheating?

:)

Update: removed redundant quotes

Update: and fixed the space after a tip from ww

Replies are listed 'Best First'.
Re^2: UK postcode regex
by Roy Johnson (Monsignor) on Apr 21, 2005 at 14:10 UTC
    A cheat that lets you avoid the /e cheat:
    s/^([A-Z]{1,2}\d{1,2}[A-Z]?)\s?(O|\d)([A-Z]{2})$/\U$1 ${\(0+$2)}$3/i

    Caution: Contents may have been coded under pressure.
Re^2: UK postcode regex
by ww (Bishop) on Apr 21, 2005 at 15:35 UTC
    Gilimanjaro: your regex strips the space between segment_one and the last_three, if one exists, whereas OP sought to insert one, if there was no space before \d[A-Za-z]{2}
      You're right! Fixed in an update...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://449987]
help
Chatterbox?
[Corion]: Discipulus: Windows isn't that complicated, at least if you look at it from the perspective of getting input via wndproc and answering to each message ;)
[Corion]: Of course, what you get put into your wndproc needs a lot of documentation ;))
Discipulus mmh maybe this is ovehelming my faculties

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-01-17 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (154 votes). Check out past polls.