[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp: named captures

Ari Brown

8/20/2007 7:44:00 PM

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



19 Answers

Sam Smoot

8/20/2007 7:54:00 PM

0

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. :)

David A. Black

8/20/2007 8:14:00 PM

0

Marcin Mielzynski

8/20/2007 9:24:00 PM

0

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

William James

8/20/2007 9:32:00 PM

0

On Aug 20, 2:44 pm, Ari Brown <a...@aribrown.com> 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"

Phrogz

8/20/2007 9:40:00 PM

0

On Aug 20, 3:32 pm, William James <w_a_x_...@yahoo.com> wrote:
> On Aug 20, 2:44 pm, Ari Brown <a...@aribrown.com> 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

Ari Brown

8/20/2007 10:00:00 PM

0


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.


Phrogz

8/20/2007 10:07:00 PM

0

On Aug 20, 4:00 pm, Ari Brown <a...@aribrown.com> 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?

Ari Brown

8/20/2007 11:17:00 PM

0

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



William James

8/21/2007 1:31:00 AM

0

On Aug 20, 4:40 pm, Phrogz <phr...@mac.com> wrote:
> On Aug 20, 3:32 pm, William James <w_a_x_...@yahoo.com> wrote:
>
> > On Aug 20, 2:44 pm, Ari Brown <a...@aribrown.com> 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

David A. Black

8/21/2007 1:46:00 AM

0