Regex for all spaces before the number

I need to select through RegEx each space in front of the number.

I know that the space is \ s and the number is \ d, but I cannot figure out how easy it is to grab the space before the number.

Sample text: John Doe 6 Jane Doe 0

It should select spaces to 6 and 0.

Any ideas?

Thanks!

+6
source share
2 answers

This regular expression will write a space before any number

\s+(?=\d) 

A positive forecast (?=\d) requires that any number of whitespace characters be followed by a digit

If you want to combine only spaces and not other characters that \s can represent, use:

 [ ]+(?=\d) 
+8
source

Try the following:

 (\s+)\d 

It captures spaces with paretes

0
source

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


All Articles