Skip to main content
Solved

StringReplacer to find last space


dos_gis
Contributor
Forum|alt.badge.img+3

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!

 

Best answer by davtorgh

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!

View original
Did this help you find an answer to your question?

3 replies

davtorgh
Contributor
Forum|alt.badge.img+10
  • Contributor
  • Best Answer
  • August 26, 2020

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!


dos_gis
Contributor
Forum|alt.badge.img+3
  • Author
  • Contributor
  • August 26, 2020

Works perfectly! Thank you.

 

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


mark2atsafe
Safer
Forum|alt.badge.img+44
  • Safer
  • August 27, 2020
davtorgh wrote:

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings