Skip to main content

Hi, Regex help please. I'm trying to replace the last space character in a list of names with a comma. Some of the names have multiple spaces in them:

 

Example:

John Doe

John Charles Doe

John Jimmy Charles Doe

 

The result I'm looking for:

John,Doe

John Charles,Doe

John Jimmy Charles,Doe

Thanks!

 

Hi,

 

I think you can set parameter 'Text To Replace' to 

\s+(\w+)$

and 'Replacement Text' to

,\1

If you suspect there may be trailing spaces, add `\s+` before `$`.

 

Hope that helps!


Works perfectly! Thank you.

 

I didn't realize that you could add the \\1 qualifier to the replacement text.


Hi,

 

I think you can set parameter 'Text To Replace' to 

\s+(\w+)$

and 'Replacement Text' to

,\1

If you suspect there may be trailing spaces, add `\s+` before `$`.

 

Hope that helps!

Interesting. There's a LastSectionOfStringRemover on the Hub that does this, but removes everything after that character. I should update it to also do a replacement, using this piece of regex. Thanks for posting a great answer.


Reply