# Regexp: getting the first found

**URL:** <https://www.ruby-forum.com/t/regexp-getting-the-first-found/178672>\
**Category:** Ruby\
**Created:** [November 9, 2009, 10:43am UTC](https://www.ruby-forum.com/t/regexp-getting-the-first-found/178672 "2009-11-09T10:43:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 9, 2009, 10:43am UTC](https://www.ruby-forum.com/t/regexp-getting-the-first-found/178672/1 "2009-11-09T10:43:08Z")

</div>

Hello,

I’m having some problem comprehending the regexp for my current need.

Example input (2 scenario):

1. 

\<\<  
/Filter  
/FlateDecode  
/Length 4169

> >

stream  
blablabla  
endstream

\<\< \>\>

1. 

\<\</Filter/FlateDecode/Length 4169\>\>  
stream  
blablabla  
endstream

\<\< \>\>

I’ve trying to get the string after the string /Filter and the first  
seen “\>\>” by using this regexp: /\<\<(._)/Filter(._)\>\>/im

The problem is that, there is two \>\> in the input and the regexp takes  
until the last \>\> it detected. How can I make it to get only until the  
first \>\> that it detects ?

Thank you in advance

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 9, 2009, 12:40pm UTC](https://www.ruby-forum.com/t/regexp-getting-the-first-found/178672/2 "2009-11-09T12:40:17Z")

</div>

Ahmad,

Newbie here so take what I have to say with a grain of salt.

Your pattern is “greedy”. To turn it into a lazy pattern add a  
question mark thusly …

/\<\<(._)/Filter(._?)\>\>/

(untested)

Ralph

Monday, November 9, 2009, 2:43:12 AM, you wrote:

AA\> Hello,

AA\> I’m having some problem comprehending the regexp for my current  
need.

AA\> Example input (2 scenario):

AA\> 1.

AA\> \<\<  
AA\> /Filter  
AA\> /FlateDecode  
AA\> /Length 4169

> > >

AA\> stream  
AA\> blablabla  
AA\> endstream

AA\> \<\< \>\>

AA\> 2.

AA\> \<\</Filter/FlateDecode/Length 4169\>\>  
AA\> stream  
AA\> blablabla  
AA\> endstream

AA\> \<\< \>\>

AA\> I’ve trying to get the string after the string /Filter and the first  
seen “\>\>\>” by using this regexp: /\<\<(._)/Filter(._)\>\>/im

AA\> The problem is that, there is two \>\> in the input and the regexp  
takes  
until the last \>\>\> it detected. How can I make it to get only until the  
first \>\>\> that it detects ?

AA\> Thank you in advance

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 9, 2009, 7:35pm UTC](https://www.ruby-forum.com/t/regexp-getting-the-first-found/178672/3 "2009-11-09T19:35:14Z")

</div>

Ralph S. wrote:

> Ahmad,
> 
> Newbie here so take what I have to say with a grain of salt.
> 
> Your pattern is “greedy”. To turn it into a lazy pattern add a  
> question mark thusly …
> 
> /\<\<(._)/Filter(._?)\>\>/
> 
> (untested)
> 
> Ralph

Thanks for the reply.

Basically that’s what I need. Thanks for pointing me towards new options  
like “greedy”. Because of that, I’ve been able to find new Ruby regex  
reference like

> **[Ruby - Regular Expressions](http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm)**
>
> Ruby Regular Expressions - A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern.

Really appreciate that, thanks
