We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I was wondering where the unusual looking syntax in the character class came from, and why it wasn't documented anywhere (in java or another language). Turns out that it is not the same thing as [a-zA-Z]! Instead, it's parsed literally, meaning "the set of characters including 'a', plus everything from 'A' to 'z' (inclusive), plus the character 'Z'". And an ASCII table (or the wiki page) shows you what's wrong:
There are six symbol characters unintentionally included! The trick seems to work, because it has all 52 (both lower and upper case) characters of the roman alphabet. Also, you can see that the 'a' and 'Z' are silently redundant, thus [aA-zZ] is identical to [A-z].
But yes, this can easily lead to faulty regular expressions. So I wanted to throw up this caution for everyone who is enjoying the (otherwise very nicely documented) pattern that sinithwar has shared.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Valid Username Regular Expression
You are viewing a single comment's thread. Return to all comments →
Warning about [aA-zZ]
I was wondering where the unusual looking syntax in the character class came from, and why it wasn't documented anywhere (in java or another language). Turns out that it is not the same thing as [a-zA-Z]! Instead, it's parsed literally, meaning "the set of characters including 'a', plus everything from 'A' to 'z' (inclusive), plus the character 'Z'". And an ASCII table (or the wiki page) shows you what's wrong:
There are six symbol characters unintentionally included! The trick seems to work, because it has all 52 (both lower and upper case) characters of the roman alphabet. Also, you can see that the
'a'
and'Z'
are silently redundant, thus[aA-zZ]
is identical to[A-z]
.But yes, this can easily lead to faulty regular expressions. So I wanted to throw up this caution for everyone who is enjoying the (otherwise very nicely documented) pattern that sinithwar has shared.