My readers really loved my previous two posts – my favorite regex of all time and my other favorite regex. I was thinking and realized I had yet another favorite regex:
[A-z]
Any idea what this regexp matches? Scroll down for the answer.
.
.
.
.
.
.
.
.
.
I bet you're not entirely sure because usually you write either [a-z]
or [A-Z]
or [a-zA-Z]
or [A-Za-z]
. But here it's [A-z]
. A is capital, and z is lowecase. I honestly thought it was a syntax error the first time I saw it. I asked some of my friends and they said it matches all letters A-Z and all letters a-z. But that is not true. It looks like it matches A-Z and a-z but in reality it matches these additional symbols: []^_,`
. It turns out that the regex [A-z]
is equal to this regex: [A-Z[]^_`a-z]
.
As always, ASCII table comes to the rescue to help us understand it. A-Z are consecutive, then for some reason some smart-ass decided we need to have these punctuation symbols there, and only then a-z follows. The range [A-z]
is colored in blue.
[A-z]
matches [A-Z[\]^_`a-z]
.
See you next time!