Note: This website has been deprecated and is no longer maintained.

Posts tagged with regex

Tuesday Tricks - Regex Posix Shortcuts

Hate typing redundant regular expressions? Me too. How often have you typed the regex [a-zA-Z0-9]?

Posix character classes are here to save the day. You can replace a-zA-Z0-9 with [:alnum:]. [:alnum:] is the posix character class and there's a whole slew of them at your disposal. Use them in Ruby like so:

'-- I have 37 dollars --' =~ /[[:alnum:]]/ #=> 3
'-- I have 37 dollars --' =~ /[[:digit:]]/ #=> 10
'-- I have 37 dollars --' =~ /[[:space:]]/ #=> 2

Note: An expression with =~ returns the first position in the string which matches the regex.

Check out the full list of posix character classes and determine how you can prettify your expressions.

This Tuesday's Trick

Posix character classes won't prevent global warming but they sure can help make your regular expressions more readable.

Posted by Mike Pack on 06/28/2011 at 01:20PM

Tags: regex, ruby, posix

Tuesday Tricks - Named Regex Groups

New in Ruby 1.9 is the ability to name capture groups so you don't have to use $1, $2...$n. First a demonstration:

Ruby Pre-1.9

regex = /(\w+),(\w+),(\w+)/
"Mike,Pack,Ruby".match regex
"First Name: #$1"
"Last Name: #$2"
"Favorite Language: #$3"

Ruby 1.9.2

regex = /(?<first_name>\w+),(?<last_name>\w+),(?<favorite_language>\w+)/
m = "Mike,Pack,Ruby".match regex
"First Name: #{m[:first_name]}"
"Last Name: #{m[:last_name]}"
"Favorite Language: #{m[:favorite_language]}"

Note: If you use named groups, Ruby won't process unnammed groups. So the following won't work:

regex = /(?<first_name>\w+),(?<last_name>\w+),(?<favorite_language>\w+),(\w+)/
m = "Mike,Pack,Ruby,Colorado".match regex
"First Name: #{m[:first_name]}"
"Last Name: #{m[:last_name]}"
"Favorite Language: #{m[:favorite_language]}"
"Location: #$4"

Note: Even though Ruby won't populate $4, it will still populate $1, $2 and $3.

This Tuesday's Trick

Perl had named regex groups, now Ruby has them. Naming your regex groups can be extremely helpful, especially when the regex becomes complex. Use 'em.

Posted by Mike Pack on 05/03/2011 at 12:58PM

Tags: tuesday tricks, regex, ruby