Skip to main content
Carl Von Stetten
Legend
February 15, 2013
Answered

Need help using Regex to parse street addresses

  • February 15, 2013
  • 2 replies
  • 1609 views

Let me start off by stating three things:

  1. I suck at regex,
  2. I've googled this and didn't come up with an answer, and
  3. I'm on ColdFusion 10

I have a form with an autocomplete input that allows a user to enter street addresses (either in full like "123 Main St, Mayberry" or in part like "123 Main St" or "Main St" or "Main St, Mayberry".  I need to tell if the user has provided a street number or not at the beginning of the value they enter into the input.  I'm trying to test the input value with this code:

<cfif ReFind("^([0-9]{1-5})",q)>

     <!--- address starts with a number --->

<cfelse>

     <!--- address doesn't start with a number --->

</cfelse>

q is the input value being passed into my code from the autocomplete function.

x should be either 0 (if the input value doesn't start with a number) or a positive integer (if the input value does start with a number).

Basically, I just need to know if the input value starts with one to five digits.

I suppose I could just do something like:

<cfif Val(q)>

Am I better off using a regex, and if so, what am I doing wrong with the one I've tried?

-Carl V.

This topic has been closed for replies.
Correct answer Adam Cameron.

It's {1,5}, not {1-5}.

Maybe read my series of articles on CF regexes to help you get better up to speed with them:

http://adamcameroncoldfusion.blogspot.co.uk/2012/12/regular-expressions-in-coldfusion-part.html

--

Adam

2 replies

Inspiring
February 15, 2013

Oh, and if you're not using the sub-expression, you don't need the parentheses.

--

Adam

Adam Cameron.Correct answer
Inspiring
February 15, 2013

It's {1,5}, not {1-5}.

Maybe read my series of articles on CF regexes to help you get better up to speed with them:

http://adamcameroncoldfusion.blogspot.co.uk/2012/12/regular-expressions-in-coldfusion-part.html

--

Adam

Carl Von Stetten
Legend
February 15, 2013

Oh man do I feel stupid!  I read the Adobe docs several times, and understood I needed a comma there, but I had a brain fart when I actually typed my code.  Thanks for the minor *headslap*!

I had tried it without the parantheses before, and had added them when trying to troubleshoot.  I'll remove them from my working code.

And I will check out your blog series on Regex.

-Carl V.