Archive for 'Uncategorized'

Matching Printable Characters

Posted on November 30, 2009, under Coding, GNU/Linux, Uncategorized.

Who doesn’t love regular expressions? They’re fucking awesome. I use’em at least 10 times per day.

Sometimes, there’re restrictions on what patterns you can use. I wanted to change a regex for validating passwords. Originally, it allowed letters, numbers, and a seemingly random collection of special characters. How lame is this?:

^[A-Za-z0-9]{1}[A-Za-z0-9_\\.\\!\@\#\-]{0,255}$

Not only are the allowed special characters arbitrary, but they’re escaping characters in a character class, and using “{1}”.

I tried changing the regex to this:

^[[:print:]]{0,255}$

Unfortunately, that wasn’t considered “valid” by the system in question. Luckily, there’s a fairly concise alternative:

^[\x20-\x7E]{0,255}$

If you find a system that lacks support for POSIX character classes, check out this Wikipedia article.