4

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?

Undo
  • 25,519
  • 37
  • 106
  • 129
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • 1
    what is the language? – cgalvao1993 May 24 '13 at 19:33
  • 1
    @CássioGalvão Regex is Regex no matter the language. And you don't parse HTML with it, either. – Undo May 24 '13 at 19:34
  • @Undo That is not entirely true ([see here](http://www.regular-expressions.info/refflavors.html)). However, in this case it's pretty safe to say that pretty much all popular regex flavors use `" "` for the space character, and `"\s"` for all whitespace characters. – ajp15243 May 24 '13 at 19:36
  • Also, the OP may find [this reference](http://www.regular-expressions.info/reference.html) helpful. – ajp15243 May 24 '13 at 19:42
  • @Undo - Where did you get the notion that "regex is regex"? There are [numerous flavors](http://www.regular-expressions.info/refflavors.html). Many have their own quirks and syntax. Check out the [tag:regex] info tab. – JDB May 29 '13 at 21:33

2 Answers2

14

" *" 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.

3

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

See reference here.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758