# Ruby Sub Regular Expression

**URL:** <https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145>\
**Category:** Ruby\
**Created:** [November 12, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145 "2008-11-12T16:55:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [November 12, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145/1 "2008-11-12T16:55:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [November 12, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145/2 "2008-11-12T17:14:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [November 12, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145/3 "2008-11-12T17:20:16Z")

</div>

On Wed, Nov 12, 2008 at 4:52 PM, Dave R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [November 12, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145/4 "2008-11-12T21:31:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [November 12, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145/5 "2008-11-12T18:21:00Z")

</div>

Thank you Jim and Jesus

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [November 14, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145/6 "2008-11-14T13:57:40Z")

</div>

On Wed, Nov 12, 2008 at 5:11 PM, Jim McKerchar  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi. You missed out your non-greedy operator (if thats the right term)

“lazy”

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [November 14, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/ruby-sub-regular-expression/153145/7 "2008-11-14T15:14:35Z")

</div>

Thanks Henrik
