johnny838
johnny838

Reputation: 932

Regular Expression Finding the Space before the Last word

I'm trying to find the space before the last word of a string.

For example, for

Bob Jane

I would want to find the space right before Jane. I am trying to do a find and replace all to make that become a comma. Thus, the final result would be

Bob ,Jane

I'm only doing this in a text editor (using Sublime) so I'm not using a programming language. Thank you!

Upvotes: 5

Views: 2812

Answers (2)

vks
vks

Reputation: 67978

[ ](?=[^ ]+$)

You can try this.Replace by ,.See demo.

https://regex101.com/r/oL9kE8/17

Upvotes: 5

Avinash Raj
Avinash Raj

Reputation: 174766

Find What:

 (\S+ *)$

REplacement string:

,\1

DEMO

If you want to add a comma after to the space which exists before the last word then try the below regex.

(?<= )(\S+ *)$

Replacement string:

,\1

DEMO

Upvotes: 1

Related Questions