Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 497
johnny838 Avatar asked Feb 23 '15 04:02

johnny838


People also ask

How do you find a space in regex?

Spaces can be found simply by putting a space character in your regex. Whitespace can be found with \s . If you want to find whitespace between words, use the \b word boundary marker.

Does \w include spaces regex?

\W means "non-word characters", the inverse of \w , so it will match spaces as well.

How do you stop space in regex?

You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^[ \t]+ and replace with nothing to delete leading whitespace (spaces and tabs). Search for [ \t]+$ to trim trailing whitespace.

Is space a special character in regex?

Regex uses backslash ( \ ) for two purposes: for metacharacters such as \d (digit), \D (non-digit), \s (space), \S (non-space), \w (word), \W (non-word). to escape special regex characters, e.g., \. for . , \+ for + , \* for * , \? for ? .


1 Answers

[ ](?=[^ ]+$)

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

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

like image 136
vks Avatar answered Oct 04 '22 18:10

vks