You have all probably seen my post on my favorite regex of all time. But here's my second favorite regex of all time:

[,-.]

Any idea what this regexp matches? Scroll down for the answer.

.

.

.

.

.

.

.

.

.

It matches a comma, dash, and dot. But why is that interesting? Because when you have a regex with a dash in the middle, it means a range, and it also means a range here. However, a comma, dash, and dot are one after another in the ASCII table, so it matches a range from the comma to the dot, which includes the dash itself! In a way it's a self-matching regex!

Take a look at the ASCII table for a quicker visual understanding. See the comma, dash, and dot characters with blue background, they come one after another.

[,-.] matches itself.

One more interesting regex!

This is not the end of this series. Here's another interesting regex. [A-z]. When I first saw it, I thought it was a syntax error. Turns out it's a valid regex but it has a surprise. Read my next post – my third favorite regex to find out what it does!

See you next time!