Page 1 of 1

Validation of House Numbers

Posted: Sun Jun 08, 2008 10:51 am
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.

Re: Validation of House Numbers

Posted: Sun Jun 08, 2008 2:27 pm
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.

Re: Validation of House Numbers

Posted: Tue Jun 10, 2008 12:13 am
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.

Re: Validation of House Numbers

Posted: Tue Jun 10, 2008 12:31 am
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.

Re: Validation of House Numbers

Posted: Tue Jun 10, 2008 9:19 am
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.

Re: Validation of House Numbers

Posted: Tue Jun 10, 2008 10:19 am
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.'

Re: Validation of House Numbers

Posted: Wed Jun 11, 2008 2:36 pm
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!!!