# Regex Non Greedy Match

**URL:** <https://rubytalk.org/t/regex-non-greedy-match/66164>\
**Category:** ruby-talk\
**Created:** [29 August 2012 01:41 UTC](https://rubytalk.org/t/regex-non-greedy-match/66164 "2012-08-29T01:41:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [29 August 2012 01:41 UTC](https://rubytalk.org/t/regex-non-greedy-match/66164/1 "2012-08-29T01:41:40Z")

</div>

Hello,

I am trying to get a non-greedy match, but am unable to do so. What may  
I be doing wrong? Please help.

> **···**
>
> # ====== Code:
> 
> puts
> 
> str1 = "Hi, @[Subject:13|My\_Subject]"  
> str2 = "Hi, @[Subject:13|My\_Subject], and @[Subject:45|Your\_Subject]"
> 
> re = /@\[Subject:\d+\|(.\*)\]/
> 
> # works fine, grabs the matching pattern  
> p " ... this works fine ..."  
> p str1.scan(re).flatten
> 
> # why does this not grab the two patterns as an array?  
> # how to avoid the greedy match?  
> # that is, how to make it a non-greedy match?  
> puts  
> p " ... this does not work fine; how to avoid the greedy match? ..."  
> p str2.scan(re).flatten
> 
> puts
> 
> # ======== Output:
> 
> " ... this works fine ..."  
> ["My\_Subject"]
> 
> " ... this does not work fine; how to avoid the greedy match? ..."  
> ["My\_Subject], and @[Subject:45|Your\_Subject"]
> 
> Thanks very much!  
> Naga Vijay
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [29 August 2012 01:48 UTC](https://rubytalk.org/t/regex-non-greedy-match/66164/2 "2012-08-29T01:48:13Z")

</div>

> re = /@\[Subject:\d+\|(.\*)\]/

try :  
re = /@\[Subject:\d+\|(.\*?)\]/

test :  
str2.scan re  
=\> [["My\_Subject"], ["Your\_Subject"]]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [29 August 2012 07:35 UTC](https://rubytalk.org/t/regex-non-greedy-match/66164/3 "2012-08-29T07:35:28Z")

</div>

In this case we can also use a character class which excludes closing  
square bracket:

irb(main):004:0\> [str1,str2].each {|s| printf "%s -\> %p\n", s,  
s.scan(/\[Subject:\d+\|([^\]]\*)\]/)}  
Hi, @[Subject:13|My\_Subject] -\> [["My\_Subject"]]  
Hi, @[Subject:13|My\_Subject], and @[Subject:45|Your\_Subject] -\>  
[["My\_Subject"], ["Your\_Subject"]]

irb(main):005:0\> [str1,str2].each {|s| printf "%s -\> %p\n", s,  
s.scan(/\[Subject:\d+\|([^\]]\*)\]/).flatten}  
Hi, @[Subject:13|My\_Subject] -\> ["My\_Subject"]  
Hi, @[Subject:13|My\_Subject], and @[Subject:45|Your\_Subject] -\>  
["My\_Subject", "Your\_Subject"]

Kind regards

robert

> **···**
>
> On Wed, Aug 29, 2012 at 3:48 AM, Regis d'Aubarede \<lists@ruby-forum.com\> wrote:
> 
> > > re = /@\[Subject:\d+\|(.\*)\]/
> > 
> > try :  
> > re = /@\[Subject:\d+\|(.\*?)\]/
> > 
> > test :  
> > str2.scan re  
> > =\> [["My\_Subject"], ["Your\_Subject"]]
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [29 August 2012 14:22 UTC](https://rubytalk.org/t/regex-non-greedy-match/66164/4 "2012-08-29T14:22:44Z")

</div>

Robert Klemme wrote in post #1073731:

> **···**
>
> > On Wed, Aug 29, 2012 at 3:48 AM, Regis d'Aubarede \<lists@ruby-forum.com\> \> wrote:
> > 
> > > > re = /@\[Subject:\d+\|(.\*)\]/
> > > 
> > > try :  
> > > re = /@\[Subject:\d+\|(.\*?)\]/
> > > 
> > > test :  
> > > str2.scan re  
> > > =\> [["My\_Subject"], ["Your\_Subject"]]
> > 
> > In this case we can also use a character class which excludes closing  
> > square bracket:
> > 
> > irb(main):004:0\> [str1,str2].each {|s| printf "%s -\> %p\n", s,  
> > s.scan(/\[Subject:\d+\|([^\]]\*)\]/)}  
> > Hi, @[Subject:13|My\_Subject] -\> [["My\_Subject"]]  
> > Hi, @[Subject:13|My\_Subject], and @[Subject:45|Your\_Subject] -\>  
> > [["My\_Subject"], ["Your\_Subject"]]
> > 
> > irb(main):005:0\> [str1,str2].each {|s| printf "%s -\> %p\n", s,  
> > s.scan(/\[Subject:\d+\|([^\]]\*)\]/).flatten}  
> > Hi, @[Subject:13|My\_Subject] -\> ["My\_Subject"]  
> > Hi, @[Subject:13|My\_Subject], and @[Subject:45|Your\_Subject] -\>  
> > ["My\_Subject", "Your\_Subject"]
> > 
> > Kind regards
> > 
> > robert
> 
> Excellent! Thanks very much, Robert!
> 
> Naga Vijay
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
