Regular expression Find the space before the last word

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

For example, for

Bob Jane 

I would like to find a space right in front of Jane. I try to find and replace everything in order to become a comma. So the end result will be

 Bob ,Jane 

I only do this in a text editor (using Sublime), so I do not use a programming language. Thanks!

+5
source share
2 answers
 [ ](?=[^ ]+$) 

You can try this.Replace by,. Watch the demo.

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

+3
source

Find what:

  (\S+ *)$ 

Spare line:

 ,\1 

Demo

If you want to add a comma after a space before the last word, try the following regular expression.

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

Replaced string:

 ,\1 

Demo

+1
source

Source: https://habr.com/ru/post/1213953/


All Articles