0

Why does \s|\t|(\r ?\n) catch single white space , but (\r ?\n)|\s |\t does not?

I was testing my GUI text input for any white space, tab or new lines and noticed this.

I was testing on https://regexr.com/ if it makes a difference.

Liam
  • 27,717
  • 28
  • 128
  • 190
Greasyjoe
  • 95
  • 1
  • 7
  • 1
    Is there supposed to be white space in the second regex? I copied the title. TBF it'd help if you put the actual regexs into the question itself – Liam Oct 02 '17 at 15:47
  • 1
    We note that the second regular expression has a space character following `\s`. So that would match a whitespace character followed by a space. – spencer7593 Oct 02 '17 at 15:48
  • Look into `RegexOptions.IgnorePatternWhitespace`. Your second regex will work the way you expect (with respect to the `"\s "` part) using this option. – mrmcgreg Oct 02 '17 at 15:52
  • @Liam I was in the middle of editing when you already did it! – Greasyjoe Oct 02 '17 at 16:20

2 Answers2

4

\s matches whitespace characters. In your first example, you have a pattern that is just |\s|*, but in the second you have |\s |* (with a space AFTER the whitespace). So the second pattern requires TWO whitespace to match.

*I added the vertical bars to help show the extra whitespace. SO dropped the space without something following it

Tyler Lee
  • 2,736
  • 13
  • 24
-3

AH spacing matters. Characters are a valid token.

(\r?\n)|\s|\t =/= (\r ?\n)|\s |\t

Greasyjoe
  • 95
  • 1
  • 7