Skip to content

Instantly share code, notes, and snippets.

@tengla
Created November 9, 2012 10:30
Show Gist options
  • Save tengla/4045046 to your computer and use it in GitHub Desktop.
Save tengla/4045046 to your computer and use it in GitHub Desktop.
Named regexp in Ruby
#!/usr/bin/env ruby -w
r = %r{name:(?<name>[A-Za-z]+);age:(?<age>\d+);location:(?<location>\w+)}x
DATA.read.split("\n").each do |row|
res = r.match(row)
puts res[:name] + " " + res[:age] + " " + res[:location]
end
__END__
name:John;age:26;location:Stockholm
name:Jane;age:25;location:Copenhagen
name:Minnie;age:5;location:Oslo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment