REgex to split a string at a capital letter in 1 variable

I am able to extract a city from a long string. Sometimes the city is 1 word sometimes multiple. When it is multiple it is written with no spaces (i.e., NewYorkCity) and I need for it to be written with spaces (i.e., New York City).

I know how to split into an array and then i can get the string value from that but I want to be able to put the city in 1 variable regardless if it is one word or multiple.

Here is an example of how the cities output are:
image

The highlighted ones are multiple name cities.

My assign for City to get to that is this:

City = System.Text.RegularExpressions.Regex.Match(NoLinePDF,“(?<=customerresidingin).+(?=,)”).ToString

City = Split(City,”,”)(0).ToString.Trim

Each word of the city name would be capitalized. Any help would be appreciated.

@atarantino

Can you try this to split.assumption is it would have capital letters

System.Text.Regularexpressions.Regex.Split(str,"(?=[A-Z])")

You can join back with space as well

String.Join(" ", System.Text.Regularexpressions.Regex.Split(str,"(?=[A-Z])").ToArray)

Cheers

Yes so that creates an array correct?

How would i combine the array back to 1 variable of string?

1 Like

Sorry didn’t see your update, i will try this.

1 Like

Hi @atarantino ,

Could you try with the below Expression :

Regex.Replace("NewYorkCity","(?=[A-Z])"," ")
1 Like

Hi @atarantino,

You can do that by using the String.Join(" ",YOURARRAYOfSTRINGVARIABLE) method.

In short (char separator, params object? values)
In your case you want a " " as separator and YOURARRAYOfSTRINGVARIABLE

this is exactly what i needed. I replaced “NewYorkCity” with the string variable. Appreciate the help.

1 Like

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