LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 06:40

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: PLD

    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.  RE: Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 07:13

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: stonysmith

    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.

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


  • 3.  RE: Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 07:21

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    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.  RE: Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 07:23

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    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.  RE: Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 07:52

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: PLD

    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.


  • 6.  RE: Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 08:11

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: stonysmith

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


  • 7.  RE: Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 08:15

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: PLD

    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.  RE: Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 08:22

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    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.  RE: Emit a list using RegexMatch

    Employee
    Posted 10-27-2014 08:27

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: PLD

    It works just fine Thanks !!!