Common Ruby Regex Patterns

Filed Under (Ruby) by adam on 27-07-2008

Tagged Under : , ,

My earlier (rather lame) post on Ruby Regex’s (Regular Expressions) is getting some Google love, so I thought I would supplement it with some more useful information.

If you are searching for Ruby Regex help, my guess is you are looking for…

Validating an email address with a Ruby Regex

Something simple like this next one will get you started.

irb(main):023:0> “me@adamloving.com”.match /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

For a much more complete email address ruby regex, try:

#
# RFC822 Email Address Regex
# --------------------------
#
# Originally written by Cal Henderson
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
#
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
#
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
# http://creativecommons.org/licenses/by-sa/2.5/
#
module RFC822
  EmailAddress = begin
    qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
    dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
    atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
      '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
    quoted_pair = '\\x5c[\\x00-\\x7f]'
    domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
    quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
    domain_ref = atom
    sub_domain = "(?:#{domain_ref}|#{domain_literal})"
    word = "(?:#{atom}|#{quoted_string})"
    domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
    local_part = "#{word}(?:\\x2e#{word})*"
    addr_spec = "#{local_part}\\x40#{domain}"
    pattern = /\A#{addr_spec}\z/
  end
end

Find URLs using a Regular Expression in Ruby

Here is a simple URL matching regular expression.

irb(main):028:0> "http://www.adamloving.com/".match /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix

Ruby boolean operator (or ruby parsing) bug

Filed Under (Ruby) by adam on 09-07-2008

Tagged Under : ,

This has bitten me a couple times in the last few days. The Ruby “or” (or equals) operator appears to have a bug where, when used in an assignment, the first value is assigned rather than the results of the entire expression.

irb(main):001:0> x = false || true
=> true
irb(main):002:0> x
=> true
irb(main):003:0> z = false or true
=> true
irb(main):004:0> z
=> false

ruby version: ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.1]

I would expect z to be true in that last example.

Zemanta Pixie

Translating c# to Ruby, Python, Java and Javascript

Filed Under (Programming) by adam on 21-06-2008

Tagged Under : , , ,

I created a Google spreadsheet to help me learn common phrases in Ruby and Python. This is an overly-ambitious project that I’ll probably never finish (I’ll have the function names learned faster than I update the spreadsheet). Anyone out there want to help? Add a comment below and I’ll ad you as a contributor to the spreadsheet.

Ruby Regex Guide

Filed Under (Ruby) by admin on 07-01-2008

Tagged Under : , ,

Regular expressions are powerful, I've never really mastered them. Here is an excellent page with help on using ruby regular expressions.