Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Ruby Sub Regular Expression (http://www.velocityreviews.com/forums/t854149-ruby-sub-regular-expression.html)

Dave Roberts 11-12-2008 03:52 PM

Ruby Sub Regular Expression
 
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.
--
Posted via http://www.ruby-forum.com/.


Jim McKerchar 11-12-2008 04:11 PM

Re: Ruby Sub Regular Expression
 
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









Dave Roberts wrote:
> 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.
>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com
> Version: 8.0.175 / Virus Database: 270.9.0/1777 - Release Date: 09/11/2008 09:53
>
>




Jesús Gabriel y Galán 11-12-2008 04:14 PM

Re: Ruby Sub Regular Expression
 
On Wed, Nov 12, 2008 at 4:52 PM, Dave Roberts <dave.a.roberts@gmail.com> wrote:
> 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.


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.


Ben Walton 11-12-2008 04:15 PM

Re: Ruby Sub Regular Expression
 



Dave Roberts 11-12-2008 05:18 PM

Re: Ruby Sub Regular Expression
 
Thank you Jim and Jesus
--
Posted via http://www.ruby-forum.com/.


Robert Klemme 11-12-2008 08:28 PM

Re: Ruby Sub Regular Expression
 
On 12.11.2008 17:14, Jesús Gabriel y Galán wrote:
> On Wed, Nov 12, 2008 at 4:52 PM, Dave Roberts <dave.a.roberts@gmail.com> wrote:
>> 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.

>
> 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


Henrik Nyh 11-14-2008 12:53 PM

Re: Ruby Sub Regular Expression
 
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"


Jim McKerchar 11-14-2008 02:11 PM

Re: Ruby Sub Regular Expression
 
Thanks Henrik

Henrik Nyh wrote:
> 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"
>
> ------------------------------------------------------------------------
>
>
> Internal Virus Database is out of date.
> Checked by AVG - http://www.avg.com
> Version: 8.0.175 / Virus Database: 270.9.0/1777 - Release Date: 09/11/2008 09:53
>
>





All times are GMT. The time now is 02:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57