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??
Asked
Active
Viewed 106 times
-2

Barmar
- 741,623
- 53
- 500
- 612

Kenny Omega
- 398
- 2
- 12
-
Because `\a` = ASCII code 7 – Lucas Trzesniewski Jan 12 '18 at 10:46
-
... and the characters from code 7 to character z (code 122) include the space and other characters. – Andy G Jan 12 '18 at 10:48
-
1One of you should post that as an answer. – Barmar Jan 12 '18 at 10:48
2 Answers
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
- 19,232
- 5
- 47
- 69