Search code examples
regexstreet-address

Regex for extracting city from comma separated address


I have an address like this:

123 Main St, Los Angeles, CA, 90210

I'd like to capture just the city:

Los Angeles

I've beeing trying tomsething like this:

(?:[^,]+),\s([^,]+)

But I don't understand how to return just the 2nd group. Using a flag like {2} seems to be including every up to the second group and not only the second group.

UPDATE

I'm using a Chrome extension that uses regex patterns, so using Javascript or another language is not possible in this case.


Solution

  • My guess is that maybe this expression might be of interest here,

    (?<=,\s)([A-Z].*?)(?=[,\s]*[A-Z]{2}[,\s]*\d{5}(?:-\d{4})?)
    

    If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.