Validation of House Numbers

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Preddy2008
Forum Newbie
Posts: 1
Joined: Sun Jun 08, 2008 10:38 am

Validation of House Numbers

Post by Preddy2008 »

Hello Regex-Pros!

I have tried to figure out how the validation of the house numbers work, but the conditional matching doesnt work for me...
I hope someone can help me out with my problem. I need to test the following input :

Valid :
4a-4b

Invalid:
4a-4a

My attempt :
^(\d+([a-z]{0,1}))-(\d+([^\2]))$

The problem is that the conditional pattern always matches.. The conditional pattern is incorrect, but I dont seem to find the bug.
I've been experimenting with the Regex-Coach, but no success.

Thanks in advance for the help.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Validation of House Numbers

Post by prometheuzz »

Preddy2008 wrote:Hello Regex-Pros!

I have tried to figure out how the validation of the house numbers work, but the conditional matching doesnt work for me...
I hope someone can help me out with my problem. I need to test the following input :

Valid :
4a-4b

Invalid:
4a-4a

My attempt :
^(\d+([a-z]{0,1}))-(\d+([^\2]))$

The problem is that the conditional pattern always matches.. The conditional pattern is incorrect, but I dont seem to find the bug.
I've been experimenting with the Regex-Coach, but no success.

Thanks in advance for the help.
You can't use back references inside character classes. Use a negative look ahead instead:

Code: Select all

^\d+([a-z])-\d+(?!\1)[a-z]$
Note that this can match numbers like 123b-123a.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Validation of House Numbers

Post by GeertDD »

prometheuzz wrote:Note that this can match numbers like 123b-123a.
And if you want to validate more stuff like that (b precedes a), I suggest to analyze the matches $1 and $2 further in PHP.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Validation of House Numbers

Post by prometheuzz »

GeertDD wrote:
prometheuzz wrote:Note that this can match numbers like 123b-123a.
And if you want to validate more stuff like that (b precedes a), I suggest to analyze the matches $1 and $2 further in PHP.
Indeed, regex is not the right tool to do all the work.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Validation of House Numbers

Post by Kieran Huggins »

I can't help but feel like validating addresses is fundamentally flawed to begin with. Especially international addresses. In England it's not uncommon for addresses to have no numbers at all, with the exception of a postal code. PO boxes are another fundamentally different format, and the list doesn't end there.

I usually collect an address in a single textarea and use the Google Maps API to try and parse it... I've had much better results that way than anything else I've ever tried.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Validation of House Numbers

Post by superdezign »

You could always send them confirmation mail similar to confirmation email, to ensure the address is correct. ;)
That won't annoy them at all. :P

'Congratulations! You have just signed up. You should be receiving a confirmation mail in order to activate your account within the next 168 hours. Make sure you tell your mailman to keep us out of your bulk mailbox.'
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Validation of House Numbers

Post by Kieran Huggins »

superdezign wrote:'Congratulations! You have just signed up. You should be receiving a confirmation mail in order to activate your account within the next 168 hours. Make sure you tell your mailman to keep us out of your bulk mailbox.'
Why is this the BEST. IDEA. EVAR?????!!1!!!
Post Reply