Sponsor

2013/02/26

Visualization of Regular Expression Character Classes - good coders code, great reuse

Visualization of Regular Expression Character Classes - good coders code, great reuse


Visualization of Regular Expression Character Classes

Posted: 25 Feb 2013 11:28 AM PST

We all know the regular expression character classes, right? There are 12 standard classes:

  [:alnum:]  [:digit:]  [:punct:]  [:alpha:]  [:graph:]  [:space:]  [:blank:]  [:lower:]  [:upper:]  [:cntrl:]  [:print:]  [:xdigit:]  

But have you seen a visual representation of what these classes match? Probably not. Therefore I created a visualization that illustrates which part of the ASCII set each character class matches. Call it a cheat sheet if you like:


small version, large version

A bunch of programs that I used

Just for my own reference, in case I ever need them again, here are the one-liners I used to create this cheat sheet:

  perl -nle 'printf "%08b - %08b\n", map { hex "0x".(split / /)[0], hex "0x".(split / /)[1] } $_ '  perl -nle 'printf "%03o - %03o\n", map { (split / /)[0], (split / /)[1] } $_'  

And I used this perl program to generate and check the red/green matches:

use warnings;  use strict;    my $red = "\e[31m";  my $green = "\e[32m";  my $clear = "\e[0m";    my ($start, $end) = @ARGV;    die 'start or end not given' unless defined $start && defined $end;    my @classes = qw/alnum alpha blank cntrl digit graph lower print punct space upper xdigit/;    for (map { chr } $start..$end) {      for my $class (@classes) {          print "${green}1${clear}" if /[[:$class:]]/;          print "${red}0${clear}" unless /[[:$class:]]/;      }      print "\n"  }  

Credits

I was inspired to create this visualization when I saw a similar table for C's ctype.h character classification functions.

No comments:

Post a Comment

Keep a civil tongue.

Label Cloud

Technology (1464) News (793) Military (646) Microsoft (542) Business (487) Software (394) Developer (382) Music (360) Books (357) Audio (316) Government (308) Security (300) Love (262) Apple (242) Storage (236) Dungeons and Dragons (228) Funny (209) Google (194) Cooking (187) Yahoo (186) Mobile (179) Adobe (177) Wishlist (159) AMD (155) Education (151) Drugs (145) Astrology (139) Local (137) Art (134) Investing (127) Shopping (124) Hardware (120) Movies (119) Sports (109) Neatorama (94) Blogger (93) Christian (67) Mozilla (61) Dictionary (59) Science (59) Entertainment (50) Jewelry (50) Pharmacy (50) Weather (48) Video Games (44) Television (36) VoIP (25) meta (23) Holidays (14)