-2

difference between [a-z] and [\a-z]. The first one does not contain space ' ' in the range but the second one is matching for space ' '. Why??

Barmar
  • 596,455
  • 48
  • 393
  • 495
Kenny Omega
  • 338
  • 1
  • 10

2 Answers2

1

[a-z] matches a character from a to z

[\a-z] matches a character from \a to z

\a is not a. According to regex101.com \a matches the bell character (ASCII 7). I don't know what this character is but it looks like whitespace.

JackPRead
  • 178
  • 8
1

\a is ASCII code 7 (bell). a-z would be the range of characters a through z.

So \a-z is the range of characters from \a to the character z (index 122) which includes the space character (and many others).

An ascii table confirms the range of characters covered.


If you wanted to allow for a backslash in addition to the characters a through z, then use [a-z\\] (the backslash needs to be escaped). [I moved it to the end as I find this version clearer.]

Andy G
  • 18,518
  • 5
  • 42
  • 63