Skip to main content
Solved

StringReplacer to find last space

  • August 26, 2020
  • 3 replies
  • 74 views

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!

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

davtorgh
Contributor
Forum|alt.badge.img+12
  • 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+59
  • Safer
  • August 27, 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!

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.