Forum: Ruby Ruby Sub Regular Expression

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.
1a18b4d281ba02cc09f6ba9c67e61f6f?d=identicon&s=25 Dave Roberts (droberts)
on 2008-11-12 16:55
Hello!  I have a string: "Hello - 1 - World"

irb(main):017:0> a = "Hello - 1 - World"
=> "Hello - 1 - World"

I want to chop off the "Hello - " part.  Hello could be any word, so I
want to match it generically.
To match the first minus I do this:

irb(main):018:0> b = a.sub(/\s-\s/, "")
=> "Hello1 - World"

Good!  Now to get rid of the first word I try this:

irb(main):019:0> c = a.sub(/.*\s-\s/, "")
=> "World"

Bad!  It matched to the second minus!  Why does sub do this?  I thought
it was supposed to match the first occurrence only.
2ac12620986aa743c74697ddc2854800?d=identicon&s=25 Jim McKerchar (Guest)
on 2008-11-12 17:14
(Received via mailing list)
Hi. You missed out your non-greedy operator (if thats the right term) in
your regex.

Try:

a = "Hello - 1 - World"

c = a.sub(/.*?\s-\s/, "")

irb(main):004:0> c = a.sub(/.*?\s-\s/, "")
=> "1 - World"


Hope that helps
Jim
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2008-11-12 17:20
(Received via mailing list)
On Wed, Nov 12, 2008 at 4:52 PM, Dave Roberts <dave.a.roberts@gmail.com>
wrote:
> => "Hello1 - World"
>
> Good!  Now to get rid of the first word I try this:
>
> irb(main):019:0> c = a.sub(/.*\s-\s/, "")
> => "World"
>
> Bad!  It matched to the second minus!  Why does sub do this?  I thought
> it was supposed to match the first occurrence only.

Because * by default is greedy, so it tries to match as much as it can.
Try this:

irb(main):002:0> c = a.sub(/.*?\s-\s/, "")
=> "1 - World"

Jesus.
1a18b4d281ba02cc09f6ba9c67e61f6f?d=identicon&s=25 Dave Roberts (droberts)
on 2008-11-12 18:21
Thank you Jim and Jesus
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-12 21:31
(Received via mailing list)
On 12.11.2008 17:14, Jesús Gabriel y Galán wrote:
>> irb(main):018:0> b = a.sub(/\s-\s/, "")
> Because * by default is greedy, so it tries to match as much as it can.
> Try this:
>
> irb(main):002:0> c = a.sub(/.*?\s-\s/, "")
> => "1 - World"

Other variants would be

irb(main):001:0> a = "Hello - 1 - World"
=> "Hello - 1 - World"
irb(main):002:0> a[/\d+\s+-.*/]
=> "1 - World"
irb(main):003:0> a.sub /^\S+\s+-\s+/, ''
=> "1 - World"
irb(main):004:0> a.sub /^\w+\s+-\s+/, ''
=> "1 - World"
irb(main):005:0>

Cheers

  robert
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2008-11-14 13:57
(Received via mailing list)
On Wed, Nov 12, 2008 at 5:11 PM, Jim McKerchar
<jim.mckerchar@btinternet.com> wrote:
> Hi. You missed out your non-greedy operator (if thats the right term)

"lazy"
2ac12620986aa743c74697ddc2854800?d=identicon&s=25 Jim McKerchar (Guest)
on 2008-11-14 15:14
(Received via mailing list)
Thanks Henrik
This topic is locked and can not be replied to.