[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[regexp] How to extract...

Jan Kowalski

4/5/2008 12:37:00 AM

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
--
Posted via http://www.ruby-....

5 Answers

7stud --

4/5/2008 1:01:00 AM

0

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
--
Posted via http://www.ruby-....

7stud --

4/5/2008 1:19:00 AM

0

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
--
Posted via http://www.ruby-....

Todd Benson

4/5/2008 8:42:00 AM

0

On Fri, Apr 4, 2008 at 8:00 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
>
> 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

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

Robert Klemme

4/5/2008 8:55:00 AM

0

On 05.04.2008 10:41, Todd Benson wrote:
> On Fri, Apr 4, 2008 at 8:00 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
>> 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
>
> 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

Todd Benson

4/5/2008 9:08:00 AM

0

On Sat, Apr 5, 2008 at 3:55 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
>
> On 05.04.2008 10:41, Todd Benson wrote:
>
> > On Fri, Apr 4, 2008 at 8:00 PM, 7stud -- <bbxx789_05ss@yahoo.com> 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