Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regexp: named captures

Reply
Thread Tools

Regexp: named captures

 
 
Ari Brown
Guest
Posts: n/a
 
      08-20-2007
How do named captures in Ruby work? This is what I've tried:

irb(main):001:0> if /(?<name>.+)/ =~ /ari/
irb(main):002:1> puts $name
irb(main):003:1> end
SyntaxError: compile error
(irb):1: undefined (?...) sequence: /(?<name>.+)/
from (irb):3

and if I do this:

irb(main):007:0> if /(<name>.+)/ =~ /ari/
irb(main):008:1> puts $name
irb(main):009:1> end
TypeError: can't convert Regexp into String
from (irb):7

I get a Regexp => String error.

Bwah?
aRi
-------------------------------------------|
Nietzsche is my copilot



 
Reply With Quote
 
 
 
 
Sam Smoot
Guest
Posts: n/a
 
      08-20-2007
Ruby doesn't have named-captures.

Coming from .NET myself, at first I was dissapointed. Eventually I
grew to love the simplicity of $1, $2, etc, but that's just me.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      08-20-2007
Hi --

On Tue, 21 Aug 2007, Sam Smoot wrote:

> Ruby doesn't have named-captures.


The new regex engine, Oniguruma, does. You can install it in 1.8, and
it's included in 1.9/2.0.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Marcin Mielżyński
Guest
Posts: n/a
 
      08-20-2007
Ari Brown pisze:
> How do named captures in Ruby work? This is what I've tried:
>
> irb(main):001:0> if /(?<name>.+)/ =~ /ari/
> irb(main):002:1> puts $name
> irb(main):003:1> end


1.9 supports that (so will 2.0):

if match = /(?<name>blah)/.match("blah")
puts match["name"]
end


lopex
 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      08-20-2007
On Aug 20, 2:44 pm, Ari Brown <(E-Mail Removed)> wrote:
> How do named captures in Ruby work? This is what I've tried:
>
> irb(main):001:0> if /(?<name>.+)/ =~ /ari/
> irb(main):002:1> puts $name
> irb(main):003:1> end
> SyntaxError: compile error
> (irb):1: undefined (?...) sequence: /(?<name>.+)/
> from (irb):3
>
> and if I do this:
>
> irb(main):007:0> if /(<name>.+)/ =~ /ari/
> irb(main):008:1> puts $name
> irb(main):009:1> end
> TypeError: can't convert Regexp into String
> from (irb):7
>
> I get a Regexp => String error.
>
> Bwah?
> aRi
> -------------------------------------------|
> Nietzsche is my copilot


A work-around:

md = "foo bar".match( /(\w+) (\w+)/ )
==>#<MatchData:0x2851ef0>
name, surname = md.captures
==>["foo", "bar"]
name
==>"foo"
surname
==>"bar"

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      08-20-2007
On Aug 20, 3:32 pm, William James <(E-Mail Removed)> wrote:
> On Aug 20, 2:44 pm, Ari Brown <(E-Mail Removed)> wrote:
> > How do named captures in Ruby work? This is what I've tried:


> A work-around:
>
> md = "foo bar".match( /(\w+) (\w+)/ )
> ==>#<MatchData:0x2851ef0>
> name, surname = md.captures
> ==>["foo", "bar"]
> name
> ==>"foo"
> surname
> ==>"bar"


Or my favorite (courtesy of Ara):

_, name, surname = /(\w+) (\w+)/.match("foo bar").to_a

 
Reply With Quote
 
Ari Brown
Guest
Posts: n/a
 
      08-20-2007

On Aug 20, 2007, at 4:13 PM, David A. Black wrote:
> On Tue, 21 Aug 2007, Sam Smoot wrote:
>
>> Ruby doesn't have named-captures.

>
> The new regex engine, Oniguruma, does. You can install it in 1.8, and
> it's included in 1.9/2.0.


I've read about Oniguruma - is it just a different engine behind
class Regex, and it won't change the syntax, right?

Because there's a Regexp wrapper I'm writing (with much help from
Robert Klemme) that uses Regex as the base, and all of the current
syntax.

~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.


 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      08-20-2007
On Aug 20, 4:00 pm, Ari Brown <(E-Mail Removed)> wrote:
> I've read about Oniguruma - is it just a different engine behind
> class Regex, and it won't change the syntax, right?
>
> Because there's a Regexp wrapper I'm writing (with much help from
> Robert Klemme) that uses Regex as the base, and all of the current
> syntax.


What do you mean by "change the syntax"?

For example, Oniguruma supports named captures (per this thread) and
zero-width negative lookbehind assertions, and ... more.

Certainly the syntax is different for new features, which cause the
current regexp engine to barf. Do you mean syntax inside the regex? Or
the syntax for declaring a regex? Or are you interested only in
backwards compatibility?

 
Reply With Quote
 
Ari Brown
Guest
Posts: n/a
 
      08-20-2007
On Aug 20, 2007, at 6:10 PM, Phrogz wrote:

> What do you mean by "change the syntax"?


I mean change the basic regexp syntax.

> For example, Oniguruma supports named captures (per this thread) and
> zero-width negative lookbehind assertions, and ... more.
>
> Certainly the syntax is different for new features, which cause the
> current regexp engine to barf. Do you mean syntax inside the regex? Or
> the syntax for declaring a regex? Or are you interested only in
> backwards compatibility?


So yea, just backwards compatibility.

Is Oniguruma behind a new class Oniguruma.new or behind Regexp.new

Sorry, but I can't seem to find much documentation on it.

Ari
-------------------------------------------|
Nietzsche is my copilot



 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      08-21-2007
On Aug 20, 4:40 pm, Phrogz <(E-Mail Removed)> wrote:
> On Aug 20, 3:32 pm, William James <(E-Mail Removed)> wrote:
>
> > On Aug 20, 2:44 pm, Ari Brown <(E-Mail Removed)> wrote:
> > > How do named captures in Ruby work? This is what I've tried:

> > A work-around:

>
> > md = "foo bar".match( /(\w+) (\w+)/ )
> > ==>#<MatchData:0x2851ef0>
> > name, surname = md.captures
> > ==>["foo", "bar"]
> > name
> > ==>"foo"
> > surname
> > ==>"bar"

>
> Or my favorite (courtesy of Ara):
>
> _, name, surname = /(\w+) (\w+)/.match("foo bar").to_a


name, surname = /(\w+) (\w+)/.match("foo bar").captures

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Screen Captures Governor Swill Computer Support 5 01-14-2005 07:28 AM
DVD captures with Hypersnap and PowerDVD. Is there anything else? Patrick D. Rockwell DVD Video 4 07-23-2004 02:14 AM
Screen captures Joshua Beall HTML 4 01-19-2004 06:10 PM
Re: Movie File screen captures to .jpg or .gif? Mike Computer Support 0 06-28-2003 01:13 PM
Re: Movie File screen captures to .jpg or .gif? Mitch Computer Support 0 06-28-2003 12:53 PM



Advertisments