# Regexp

**URL:** <https://www.ruby-forum.com/t/regexp/97487>\
**Category:** Ruby\
**Created:** [July 7, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/regexp/97487 "2006-07-07T12:44:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![outrelouxhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@outrelouxhe](https://www.ruby-forum.com/u/outrelouxhe)\
**Post date:** [July 7, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/regexp/97487/1 "2006-07-07T12:44:10Z")

</div>

Hello,

I’m new to Ruby and I’m fighting with regexp.

I would like to replace ‘test1\<\math\>’ and ‘test2\<\math\>’ by  
‘ok’ in the string “blabla1 blabla1 test1\<\math\> blabla2 blabla2  
test2\<\math\>”

When I try this :

@a = “blabla1 blabla1 test1\<\math\> blabla2 blabla2  
test2\<\math\>”  
@b = @a.gsub(/.+\<\math\>/,‘ok’)

I get “blabla1 blabla1 ok” instead of “blabla1 blabla1 ok blabla2  
blabla2 ok”

Could you please help me to tune my regexp so it works the way I want ?

Thank you in advance,

Fred.

---

<div class="post-metadata">

**Author:** ![outrelouxhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@outrelouxhe](https://www.ruby-forum.com/u/outrelouxhe)\
**Post date:** [July 7, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/regexp/97487/2 "2006-07-07T12:52:55Z")

</div>

fr fred:

# When I try this :

# 

# @a = "blabla1 blabla1 test1\<\math\> blabla2 blabla2

# test2\<\math\>"

# @b = @a.gsub(/.+\<\math\>/,‘ok’)

# 

# I get “blabla1 blabla1 ok” instead of "blabla1 blabla1 ok blabla2

# blabla2 ok"

# 

# Could you please help me to tune my regexp so it works the

# way I want ?

your code works fine here,

irb(main):001:0\> @a = “blabla1 blabla1 test1\<\math\> blabla2  
blabla2  
irb(main):002:0” test2\<\math\>"  
=\> “blabla1 blabla1 test1 blabla2 blabla2  
\ntest2”  
irb(main):003:0\> @b = @a.gsub(/.+\<\math\>/,‘ok’)  
=\> “blabla1 blabla1 ok blabla2 blabla2 \nok”

(ignore the newline; i just copiednpaste fr your email…)

kind regards -botp

---

<div class="post-metadata">

**Author:** ![outrelouxhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@outrelouxhe](https://www.ruby-forum.com/u/outrelouxhe)\
**Post date:** [July 7, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/regexp/97487/3 "2006-07-07T12:56:12Z")

</div>

Hi –

On Fri, 7 Jul 2006, Fred VD wrote:

> @a = “blabla1 blabla1 test1\<\math\> blabla2 blabla2  
> test2\<\math\>”  
> @b = @a.gsub(/.+\<\math\>/,‘ok’)
> 
> I get “blabla1 blabla1 ok” instead of “blabla1 blabla1 ok blabla2  
> blabla2 ok”
> 
> Could you please help me to tune my regexp so it works the way I want ?

As I suspect seven or eight people will tell you, you need to make  
your one-or-more quantifier non-greedy 🙂 Note the question mark:

a = “blabla1 blabla1 test1\<\math\> blabla2 blabla2  
test2\<\math\>”  
puts a.gsub(/.+?\<\math\>/,‘ok’)

I’ve made a couple of other changes. In a double-quoted string, “\m”  
is just the letter m. If you want a \ you have to escape it with  
another . (Are you sure you don’t want a forward slash anyway?)

I have a feeling you don’t really need instance variables here; if  
not, it’s better to use locals.

David

---

<div class="post-metadata">

**Author:** ![outrelouxhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@outrelouxhe](https://www.ruby-forum.com/u/outrelouxhe)\
**Post date:** [July 7, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/regexp/97487/4 "2006-07-07T12:56:22Z")

</div>

On 7/7/06, Fred VD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @a = “blabla1 blabla1 test1\<\math\> blabla2 blabla2  
> test2\<\math\>”  
> @b = @a.gsub(/.+\<\math\>/,‘ok’)
> 
> I get “blabla1 blabla1 ok” instead of “blabla1 blabla1 ok blabla2  
> blabla2 ok”
> 
> Could you please help me to tune my regexp so it works the way I want ?

You need to replace .+ with .+? - the former is ‘greedy’; i.e. it  
matches as much as it possibly can, so it matches everything between  
the first and the last \<\math\>.

martin

---

<div class="post-metadata">

**Author:** ![outrelouxhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@outrelouxhe](https://www.ruby-forum.com/u/outrelouxhe)\
**Post date:** [July 7, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/regexp/97487/5 "2006-07-07T12:56:32Z")

</div>

On 07/07/06, Fred VD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @a = “blabla1 blabla1 test1\<\math\> blabla2 blabla2  
> test2\<\math\>”  
> @b = @a.gsub(/.+\<\math\>/,‘ok’)
> 
> I get “blabla1 blabla1 ok” instead of “blabla1 blabla1 ok blabla2  
> blabla2 ok”

- and \* are greedy. Use +? and \*? for non-greedy operation:

a = ‘a b c d e f g’  
a.gsub(%r!.+?!, ‘ok’)  
=\> “a b ok d ok f g”  
a.gsub(%r!.+!, ‘ok’)  
=\> “a b ok f g”

A greedy expression will match up to the \*last \* occurrence of what  
follows it in the expression, whereas a non-greedy expression will  
match up to the _first_ occurrence.

Paul.

---

<div class="post-metadata">

**Author:** ![outrelouxhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@outrelouxhe](https://www.ruby-forum.com/u/outrelouxhe)\
**Post date:** [July 7, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/regexp/97487/6 "2006-07-07T14:32:08Z")

</div>

Yes ! The “+?” was what I needed !  
Thanks to all for your help.

Fred.

---

<div class="post-metadata">

**Author:** ![outrelouxhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@outrelouxhe](https://www.ruby-forum.com/u/outrelouxhe)\
**Post date:** [July 7, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/regexp/97487/7 "2006-07-07T13:08:48Z")

</div>

On Jul 7, 2006, at 11:52, Peña, Botp wrote:

> # Could you please help me to tune my regexp so it works the
> 
> =\> “blabla1 blabla1 ok blabla2 blabla2 \nok”
> 
> (ignore the newline; i just copiednpaste fr your email…)

I thought this might warrant some explanation in light of the later  
posts about greedy operators.

The newline from the paste is what makes this work differently from  
the normal example - greedy expressions (already mentioned) aren’t  
greedy across newlines unless you specify a multiline regex, so the  
newline is what made the regex appear correct, when it wouldn’t be in  
a general case.

# with newline

a = “blabla1 blabla1 test1 blabla2 blabla2  
\ntest2”  
a.gsub(/.+\<\math\>/,‘ok’)  
=\> “blabla1 blabla1 ok blabla2 blabla2 \nok”

# sans newline

b = “blabla1 blabla1 test1 blabla2 blabla2  
test2”  
b.gsub(/.+\<\math\>/,‘ok’)  
=\> “blabla1 blabla1 ok”

matthew smillie.
