Will M
Will M

Reputation: 2145

Regex for all spaces before a number

I need to select via RegEx every space before a number.

I know whitespace is \s and digit is \d, but I can't figure out how to just grab the space before the number.

Sample text: John Doe 6 Jane Doe 0

It should select the spaces before 6 and 0.

Any ideas?

Thanks!

Upvotes: 10

Views: 14976

Answers (2)

Ro Yo Mi
Ro Yo Mi

Reputation: 15010

This regex will capture the space before any number

\s+(?=\d)

The positive look ahead (?=\d) requires that any number of whitespace characters be followed by a digit

If you want to match only spaces and not the other characters which could be represented by a \s then use:

[ ]+(?=\d)

Upvotes: 21

gitaarik
gitaarik

Reputation: 46350

Try this one:

(\s+)\d

It captures the spaces with the paretheses

Upvotes: -1

Related Questions