Forum: Ruby Regexp madness

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Damphyr (Guest)
on 2006-04-01 19:33
(Received via mailing list)
OK you Regexp gurus out there (has anyone noticed the relative size of
regexp threads -anything with regexp seems to excite lots of attention).

I have a puzzler. I've been combing the ruby-talk archives trying to
convert the BlogRD format to Textile.
BlogRD marks links with ((< >)) and I thought it would be simple to use
the pattern /\(\(<(.+)>\)\) to get the links.

Problem:

Having two links in the same block of text completely screws up the
results:

TXT="((< Link one >)) and a bit further down ((< Link two >))"
LINK=/\(\(<(.+)>\)\)/

TXT=~LINK
p $1
---
" Link one >)) and a bit further down ((< Link two "

I was expecting " Link one "
Why the result? Where id I go wrong?
V.-
P.S.
Take your time,I solved the parsing problem another way.
--
http://www.braveworld.net/riva
Chris A. (Guest)
on 2006-04-01 20:15
(Received via mailing list)
I just answered this is the other regexp thread.  If you put a ?
you're regexp becomes non-greedy

LINK=/\(\(<(.+?)>\)\)/
James G. (Guest)
on 2006-04-01 20:55
(Received via mailing list)
On Apr 1, 2006, at 9:31 AM, Damphyr wrote:

> Why the result? Where id I go wrong?

I see you already got an answer to the other question, I will tackle
this one.  In a Regexp, .+ means one or more of anything, but (and
here is the kicker) as many as possible.  It will keep eating
characters as long as it can, with the expression still being true.

See Chris reply for how to turn it into one or more of anything, but
as few as possible.

James Edward G. II
Damphyr (Guest)
on 2006-04-01 22:24
(Received via mailing list)
Chris A. wrote:
> I just answered this is the other regexp thread.  If you put a ?
> you're regexp becomes non-greedy
>
> LINK=/\(\(<(.+?)>\)\)/
>
Yeap, it had to be something like this. Nice, thanks. Now I know.
Oh well, I'll just change the code once more, regexps make it so much
more compact :)
V.-

--
http://www.braveworld.net/riva
This topic is locked and can not be replied to.