« The iPhone Book is in Beta! | Main | Ruby 1.9.1 Preview 1 is out »

October 16, 2008

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e20105358b7236970b

Listed below are links to weblogs that reference Fun with Ruby 1.9 Regular Expressions:

Comments

Russell Jones

Great stuff Dave, can't wait to play around with that. Thanks for sharing.

Justin George

Interesting, looks a lot like a parser combinator method of parsing e.g. Parsec for Haskell.

Piers Cawley

Tiny stylistic quibble, but it's become common practice in the Perl community when writing complex patterns like this to do something like %r{(?x) ... } rather than %r{ ... }x. It gets the pragmas up front so you can understand the executing context before you start trying to understand what the expression means.

Bil Kleb

You might consider having an example search for Orwell's "verbal false limbs"? http://www.calvinvanhoek.com/articles/2007/04/politics-english-language/

Wolfgang Nádasi-Donner (WoNáDo)

I've written several samples for a german blog (Ruby-Mine) using Oniguruma in Ruby 1.9. Unfortunately the associated texts are germal only (I was too lazy to translate it before Ruby 1.9.1 is stable), but maybe you can use some of them for your book. If you are interested, I will send them (together with some english remarks) and you can use them for your book.

Here is one example - A pocket calculator. The basic part can be used for verifying input data in GUI fields.

pattern = / (?\g\+\g|\g-\g|\g){0}
(?|\g\*\g|\g\/\g|\g){0}
(?[-+]?\g|\(\g\)){0}
(?\g|\g){0}
(?[a-zA-Z_]\w*){0}
(?\d+(\.\d+)?){0}
^((?\g)=)?(?\g)$
/x

vars = Hash.new(0)
basbind = binding

# print 'input> ' # for interactive usage only
while (!(inp = DATA.gets).chomp.match(/^quit$/i))
if (md = inp.chomp.gsub(/\s+/,'').match(pattern))
expr = md[:expr].gsub(/([a-zA-Z_]\w*)/, 'vars["\1"]')
erg = eval(expr, basbind)
vars[md[:var]] = erg if md[:var]
puts "#{inp.chomp}, Result> #{(md[:var])?(md[:var]+'='):''}#{erg}"
else
puts "+++++ Incorrect input: '#{inp.chomp}'"
end
# print 'input> ' # for interactive usage only
end
puts '***** Show variables *****'
vars.keys.sort.each{|v|puts "#{v}=#{vars[v]}"}
puts '******* End ********'
__END__
30+12
a = 30 + 12
b = 2*a
c = -(a*a+5)
d = (6+5*a)*c
quit

Mario Aquino

Dave,

That is an impressive set of examples and I want to understand them better. In the second sentence, md[:adjective] returns the last adjective in the string. Is there a way to get the first one ('big')?

I'm looking forward to the final version of the book! Thanks.

Mario Aquino

Also, I just tried to run your example in my local build of 1.9 (1.9.0-3_1) and it failed. The error message I get is: 'regex.rb:13:in `': undefined method `[]' for nil:NilClass (NoMethodError)'

Which 1.9 build are you using for this example?

Dave Thomas

I'm using

ruby 1.9.0 (2008-10-14 revision 15427) [i386-darwin9.5.0]

I'm guessing a simply typo, so the pattern didn't match, and the match returned nil.

Can you access the first named match? Not using the [:adjective] syntax. For some reason, Ruby lets you index into numbered matches, but not named ones.


Dave

The comments to this entry are closed.

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now available
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.