Marty Wallace
Marty Wallace

Reputation: 35734

Regex difference between " *" and "\s*"

I'm using regular expressions to match any number of white space characters. Is there a difference between the following?

" *"
"\s*"

Are there special cases where either would cause an issue?

Upvotes: 4

Views: 1418

Answers (2)

user177800
user177800

Reputation:

" *" will only match zero or more of the [SPACE] character and only the [SPACE] character.

"\s*" will match zero or more of ANY whitespace character ( SPACE, TAB, FORMFEED, etc. ).

Therefore they are not equivalent expressions. \s is what you want usually, especially since you can't readily see whitespace characters by definition.

Upvotes: 14

Denys Séguret
Denys Séguret

Reputation: 382132

This is the difference between the regular space () and any space (space, tab, line breaks, etc.).

See reference here.

Upvotes: 3

Related Questions