Results 1 to 9 of 9

Thread: Emit a list using RegexMatch

  1. #1

    Default Emit a list using RegexMatch

    Hi,

    I am currently struggling with RegexMatch. I would like to be able to extract the ZIP Code from an Address string.

    Example:
    Address = "317 Autumn street, 92131 San Diego Califoria USA"
    x = regexMatch(Address, "\\d{5}")
    emit x

    It doesn't work because regexMatch returns a list I guess, is there a way to do this?

    Thanks

  2. #2
    Lavastorm Employee stonysmith's Avatar
    Join Date
    Nov 2006
    Location
    Grapevine Tx
    Posts
    799

    Default

    The code below should work for you.
    I've added a bit to catch addresses such as this:
    12345 Main Street, Los Angeles, CA, 90210

    This would not properly handle.
    12345 Main Street, APT 98765, Los Angeles, CA, 90210
    You'll need to add another IF to handle 5 digit apartments.

    Code:
    x = regexMatch(Address, "\\d{5}")
    if len(x)>1 then y=x[1][0].str() else y=x[0][0].str()
    
    emit y

  3. #3
    Lavastorm Employee
    Join Date
    Dec 2006
    Location
    Dallas, TX
    Posts
    297

    Default

    Just a quick thought about how you might handle an address that has multiple matches to \\d{5}.

    These matches will be in an array and you'd likely want to use "len()-1" in an expression to get to the last entry of the array. This is assuming the zip will always be the last 5 digit number matched in the address.

  4. #4
    Lavastorm Employee
    Join Date
    Dec 2006
    Location
    Dallas, TX
    Posts
    297

    Default

    One other point.
    Use this emit statement to retrieve the current contents of an array so you can figure out how to access it:

    emit str(y) as y

  5. #5

    Default

    Thanks guys for the quick reply. It works fine when there is just one match, but when there is more than one match the results are not accurate anymore.

    Example: "13040-13041 Sunset Ways Road, Reston, Virginia 20150 , USA"

    It returns the second match (13041), not the last one. It may be simple but I cannot figure out how to change the code to handle these exceptions.
    Last edited by PLD; 10-27-2014 at 02:56 PM.

  6. #6
    Lavastorm Employee stonysmith's Avatar
    Join Date
    Nov 2006
    Location
    Grapevine Tx
    Posts
    799

    Default

    If you used my version above with the IF statement.. it should return 20150 for that address.

  7. #7

    Default

    It is a bit weird because I used exactly your version but for some reason it keeps returning the second match. I am still trying to figure why.

  8. #8
    Lavastorm Employee
    Join Date
    Dec 2006
    Location
    Dallas, TX
    Posts
    297

    Default

    How about this:

    Address = "13040-13041 Sunset Ways Road, Reston, Virginia 20150 , USA"
    x = regexMatch(Address, "\\d{5}")
    zip = str(x[len(x)-1][0])
    emit zip

  9. #9

    Default

    It works just fine Thanks !!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •