# \[regexp\] How to extract

**URL:** <https://www.ruby-forum.com/t/regexp-how-to-extract/133928>\
**Category:** Ruby\
**Created:** [April 5, 2008, 2:37am UTC](https://www.ruby-forum.com/t/regexp-how-to-extract/133928 "2008-04-05T02:37:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![halfbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7ab992/32.png) [@halfbyte](https://www.ruby-forum.com/u/halfbyte)\
**Post date:** [April 5, 2008, 2:37am UTC](https://www.ruby-forum.com/t/regexp-how-to-extract/133928/1 "2008-04-05T02:37:00Z")

</div>

From:

#header h1 a {  
display: block;  
background: url(images/wordpress-logo.png) center left no-repeat;  
width: 301px;  
height: 88px;  
text-indent: -9999px;  
float: left;  
}

#header ul li#download a {  
background: #d54e21 url(images/download-tab-bg.png) bottom left  
repeat-x;  
color: #fff;  
-moz-border-radius-topleft: 3px;  
-khtml-border-top-left-radius: 3px;  
-webkit-border-top-left-radius: 3px;  
border-top-left-radius: 3px;  
-moz-border-radius-topright: 3px;  
-khtml-border-top-right-radius: 3px;  
-webkit-border-top-right-radius: 3px;  
border-top-right-radius: 3px;  
text-shadow: #b5421c 1px 1px 1px;  
}

i need each occurence of text enclosed between url(\*\*\*\*\*\*\*\*\*\*\*\*), for  
this example:

images/wordpress-logo.png  
images/download-tab-bg.png

---

<div class="post-metadata">

**Author:** ![halfbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7ab992/32.png) [@halfbyte](https://www.ruby-forum.com/u/halfbyte)\
**Post date:** [April 5, 2008, 3:00am UTC](https://www.ruby-forum.com/t/regexp-how-to-extract/133928/2 "2008-04-05T03:00:58Z")

</div>

Jan Kowalski wrote:

> From:
> 
> #header h1 a {  
> display: block;  
> background: url(images/wordpress-logo.png) center left no-repeat;  
> width: 301px;  
> height: 88px;  
> text-indent: -9999px;  
> float: left;  
> }
> 
> #header ul li#download a {  
> background: #d54e21 url(images/download-tab-bg.png) bottom left  
> repeat-x;  
> color: #fff;  
> -moz-border-radius-topleft: 3px;  
> -khtml-border-top-left-radius: 3px;  
> -webkit-border-top-left-radius: 3px;  
> border-top-left-radius: 3px;  
> -moz-border-radius-topright: 3px;  
> -khtml-border-top-right-radius: 3px;  
> -webkit-border-top-right-radius: 3px;  
> border-top-right-radius: 3px;  
> text-shadow: #b5421c 1px 1px 1px;  
> }
> 
> i need each occurence of text enclosed between url(\*\*\*\*\*\*\*\*\*\*\*\*), for  
> this example:
> 
> images/wordpress-logo.png  
> images/download-tab-bg.png

str = \<\<CSS  
#header h1 a {  
display: block;  
background: url(images/wordpress-logo.png) center left no-repeat;  
width: 301px;  
height: 88px;  
text-indent: -9999px;  
float: left;  
}

#header ul li#download a {  
background: #d54e21 url(images/download-tab-bg.png) bottom left  
repeat-x;  
color: #fff;  
-moz-border-radius-topleft: 3px;  
-khtml-border-top-left-radius: 3px;  
-webkit-border-top-left-radius: 3px;  
border-top-left-radius: 3px;  
-moz-border-radius-topright: 3px;  
-khtml-border-top-right-radius: 3px;  
-webkit-border-top-right-radius: 3px;  
border-top-right-radius: 3px;  
text-shadow: #b5421c 1px 1px 1px;  
}  
CSS

pattern = /url((.\*))/ #to match a parenthesis escape it with a ‘’

str.each do |line|  
match = line[pattern, 1] #1 is the parenthesized sub pattern  
if match  
puts match  
end  
end

–output:–  
images/wordpress-logo.png  
images/download-tab-bg.png

---

<div class="post-metadata">

**Author:** ![halfbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7ab992/32.png) [@halfbyte](https://www.ruby-forum.com/u/halfbyte)\
**Post date:** [April 5, 2008, 10:41am UTC](https://www.ruby-forum.com/t/regexp-how-to-extract/133928/3 "2008-04-05T10:41:58Z")

</div>

On Fri, Apr 4, 2008 at 8:00 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > float: left;  
> > -moz-border-radius-topright: 3px;  
> > images/download-tab-bg.png  
> > }  
> > -khtml-border-top-right-radius: 3px;  
> > if match  
> > puts match  
> > end  
> > end
> 
> –output:–
> 
> images/wordpress-logo.png  
> images/download-tab-bg.png

You can use a non-greedy multi-line #scan pattern as well…

str.scan /url((.\*?))/m  
=\> [[“images/wordpress-logo.png”], [images/download-tab-bg.png]]

…which you can #flatten if you want.

Todd

---

<div class="post-metadata">

**Author:** ![halfbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7ab992/32.png) [@halfbyte](https://www.ruby-forum.com/u/halfbyte)\
**Post date:** [April 5, 2008, 3:19am UTC](https://www.ruby-forum.com/t/regexp-how-to-extract/133928/4 "2008-04-05T03:19:02Z")

</div>

7stud – wrote:

> pattern = /url((.\*))/ #to match a parenthesis escape it with a ‘’

Actually, to be safe make the .\* non-greedy:

pattern = /url((.\*?))/ #to match a parenthesis escape it with a ‘’

Here’s the difference:

str = “abcurl(good)bad)xyz”

pattern1 = /url((._))/ #to match a parenthesis escape it with a ‘’  
pattern2 = /url((._?))/ #to match a parenthesis escape it with a ‘’

match1 = str[pattern1, 1] #1 is the parenthesized sub pattern  
puts match1

match2 = str[pattern2, 1] #1 is the parenthesized sub pattern  
puts match2

–output:–  
good)bad  
good

---

<div class="post-metadata">

**Author:** ![halfbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7ab992/32.png) [@halfbyte](https://www.ruby-forum.com/u/halfbyte)\
**Post date:** [April 5, 2008, 10:55am UTC](https://www.ruby-forum.com/t/regexp-how-to-extract/133928/5 "2008-04-05T10:55:31Z")

</div>

On 05.04.2008 10:41, Todd B. wrote:

> > > float: left;  
> > > -moz-border-radius-topright: 3px;  
> > > images/download-tab-bg.png  
> > > }  
> > > -khtml-border-top-right-radius: 3px;  
> > > if match  
> > > You can use a non-greedy multi-line #scan pattern as well…
> 
> str.scan /url((.\*?))/m  
> =\> [[“images/wordpress-logo.png”], [images/download-tab-bg.png]]
> 
> …which you can #flatten if you want.

I’d rather use a more specific match as I believe this could be a tad  
more efficient

str.scan /url(([^)]\*))/m

Cheers

robert

---

<div class="post-metadata">

**Author:** ![halfbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7ab992/32.png) [@halfbyte](https://www.ruby-forum.com/u/halfbyte)\
**Post date:** [April 5, 2008, 11:08am UTC](https://www.ruby-forum.com/t/regexp-how-to-extract/133928/6 "2008-04-05T11:08:27Z")

</div>

On Sat, Apr 5, 2008 at 3:55 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 05.04.2008 10:41, Todd B. wrote:
> 
> > On Fri, Apr 4, 2008 at 8:00 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d rather use a more specific match as I believe this could be a tad more  
> efficient
> 
> str.scan /url(([^)]\*))/m

Yep, I think that’s better.

Todd
