Regex Extracting City Name

Hi all,

I have a string:
" City: Birmingham City: St. Louis"

I want to match both city names… in this case, “Birmingham” and “St. Louis”

I figured I’d use a regex that would look for the text "City: " and retrieve what comes after that until it hits either "City: " again or the end of the line. Then I’d just trim off any extra spaces around my matches.

I have the following Regex expression that isn’t quite getting me what I want:
(?<=City: ).+((?=City: )|$)

It matches from the first city to the end of the line

Any help would be greatly appreciated!

Maybe this helps:

1 Like

Hi,

How about the following pattern?

(?<=City: ).+?\S(?=\s*City:|\s*$)

Regards,

1 Like

Try something like this using a assign activity

use a assign activity like this

str_matches = System.Text.RegularExpressions.Regex.Matches(strinput.ToString, "City: (.+?)(?=(?: City:|\z))")

where str_matches is a variable of type System.Text.RegularExpressions.MatchCollection
to get each value in assign activity

str_city_1 = str_matches(0).Group(1).Value

another assign

str_city_2 = str_matches(1).Group(1).Value

Cheers @Caleb_D2

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.