Specification of Ruby regex?

R

Ronald Pijnacker

Hi all,

I was just wondering... Is there any place where Ruby's Regex
capabilities are described?

E.g. it seems that /\w{3}/ matches at least three consecutive characters,
but I do not seem to be able to locate any exact documentation on this.

Any idea's?

Ronald.
 
T

Tim Hunter

There is certainly a lot of information there, but I have the feeling that
there are things not discussed. My example "r {m}" is not mentioned as
such, but works anyway.

A better example would have been /o . I have seen it being used, but it is
not documented. If it does what I've been told it does, it is good to know
about.

Another is /(?: ...)/ . It seems to work, but also is not documented.
Aparently the Pickaxe book is not exhaustive.

As I am currently reading "Mastering Regular Expressions", I started
wondering what exacly is or is not supported by Ruby.

Ronald.

In the paper version of the Pickaxe r{m} is described on
page 61. Extensions such as (?:...) on pp. 209-211.

My online version of the Pickaxe (from ruby-doc.org) documents both as
well. For the extensions, click The Ruby Language in the TOC and scroll
down to the Extensions section. Repetition (r{m}) is documented in
Standard Types.
 
G

Gavin Sinclair

btw, since there is a thread about that, i wanted to ask:
does ruby support named matches (sorry i don't know the proper terminology)?
C# does it like this:
"(?<year>\d{4})-(?<month>\d{1,2})-(?<day>\d{1,2})"
matches "2002-4-6"
and then in my match groups i have "year", "month", "day".
(looked in pickaxe + google ruby "regexp match group")

I'm 99.99% sure it doesn't.

Gavin
 
M

Mark Slagell

Gavin said:
I'm 99.99% sure it doesn't.

Gavin

Is this helpful at all?

year, month, day =
/(\d{4})-(\d{1,2})-(\d{1,2})/.match(s).to_a


(where s is the string to be matched)
 
F

Florian Frank

year, month, day =
/(\d{4})-(\d{1,2})-(\d{1,2})/.match(s).to_a
(where s is the string to be matched)


You probably meant to write this:

year, month, day = /(\d{4})-(\d{1,2})-(\d{1,2})/.match(s).captures
 
M

Mark Slagell

Florian said:
You probably meant to write this:

year, month, day = /(\d{4})-(\d{1,2})-(\d{1,2})/.match(s).captures

um, no, I wrote what I meant, but is something wrong with to_a there?
 
T

ts

M> um, no, I wrote what I meant, but is something wrong with to_a there?

it add $&

svg% ruby -e 'p /.(.)/.match("ab").to_a'
["ab", "b"]
svg%

svg% ruby -e 'p /.(.)/.match("ab").captures'
["b"]
svg%




Guy Decoux
 
X

Xavier Noria

m Multiline Mode. Normally, ``.'' matches any character except
a newline. With the /m option, ``.'' matches any character.

Now that we are on it and just out of curiosity is there any particular
reason /m is Perl's /s?

On the other hand, the interpreter does not complain with /s so looks
like an undocumented (AFAIK) option. If it is public, what's its
meaning?

-- fxn
 
H

Hal Fulton

Ronald said:
Hi,

Thanks for all the feedback. Apparently I have to increase my search
capabilities in ProgrammingRuby, because there are things documented
that I could not find :( .

There's a reasonably good summary in chapter 1 of _The Ruby Way_.
Only a page or two as I recall, but it does have one or two items
not in the Pickaxe.

Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top