[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Regular expressions - Again

J. mp

3/6/2007 12:01:00 AM

I'm really bad with this things called regular expressions, so I'm
looking for help again.

Now, if I have a String like
"some string some content <title>this I want</title>"

And I want to use the scan function to extract what is between <title>
and </title> how can I build my regular expression. The final result
should be:
this I want

Thnaks

--
Posted via http://www.ruby-....

12 Answers

Gavin Kistner

3/6/2007 12:17:00 AM

0

On Mar 5, 5:00 pm, "J. mp" <joaomiguel.pere...@gmail.com> wrote:
> Now, if I have a String like
> "some string some content <title>this I want</title>"
>
> And I want to use the scan function to extract what is between <title>
> and </title> how can I build my regular expression. The final result
> should be:
> this I want

irb(main):001:0> str = "This is <title>what I
irb(main):002:0" want</title> and no more"
=> "This is <title>what I\nwant</title> and no more"
irb(main):003:0> str[ %r{<title>(.+?)</title>}, 1 ]
=> nil
irb(main):004:0> str[ %r{<title>(.+?)</title>}m, 1 ]
=> "what I\nwant"

Note that the use of 'm' to match across multiple lines, assuming your
title tag spans them.

Note that this will fail if you have "<title>This is <title>nested</
title> content</title>", and will result in "This is <title>nested"

Jenda Krynicky

3/7/2007 1:49:00 PM

0

J. mp wrote:
> I'm really bad with this things called regular expressions, so I'm
> looking for help again.
>
> Now, if I have a String like
> "some string some content <title>this I want</title>"
>
> And I want to use the scan function to extract what is between <title>
> and </title> how can I build my regular expression. The final result
> should be:
> this I want
>
> Thnaks

You generaly want to use a HTML parser ... provided that Wuby has one.

You may be lucky with <title> since it's likely to not include any
attributes, but still there might be some whitespace INSIDE the tags,
there may be a comment inside the <title>...</title> that you may or may
not want, there may be a <title> or </title> inside a comment etc. etc.
etc.

In (censored) I'd use HTML::Parser from CPAN, but shhhh ... this is a
Wuby site, we don't speak of such things here.

Jenda

--
Posted via http://www.ruby-....

J. mp

3/7/2007 2:00:00 PM

0


> You generaly want to use a HTML parser ... provided that Wuby has one.
>
> You may be lucky with <title> since it's likely to not include any
> attributes, but still there might be some whitespace INSIDE the tags,
> there may be a comment inside the <title>...</title> that you may or may
> not want, there may be a <title> or </title> inside a comment etc. etc.
> etc.
>
> In (censored) I'd use HTML::Parser from CPAN, but shhhh ... this is a
> Wuby site, we don't speak of such things here.
>
> Jenda
I ended with Hpricot, it's working fine with a few tests I made till
now.


--
Posted via http://www.ruby-....

Harry

3/7/2007 2:06:00 PM

0

> You may be lucky with <title> since it's likely to not include any
> attributes, but still there might be some whitespace INSIDE the tags,
>
> Jenda
>

str = "This is <title> what I\n\n\n\n \n want </title> and no more"
p str

str =~ /<title>(.*?)<\/title>/m
p $1.gsub(/(\n|\s)+/, " ").strip


--

http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

Alex Young

3/7/2007 2:08:00 PM

0

Harry wrote:
>> You may be lucky with <title> since it's likely to not include any
>> attributes, but still there might be some whitespace INSIDE the tags,
>>
>> Jenda
>>
>
> str = "This is <title> what I\n\n\n\n \n want </title> and
> no more"
> p str
>
> str =~ /<title>(.*?)<\/title>/m
> p $1.gsub(/(\n|\s)+/, " ").strip
>
>
I think he meant:

str = "This is <title >what I want</title> and no more"

but we don't know if the problem requires handling anything more complex
than simple tags.

--
Alex

Alex Young

3/7/2007 2:09:00 PM

0

Jenda Krynicky wrote:
> J. mp wrote:
>> I'm really bad with this things called regular expressions, so I'm
>> looking for help again.
>>
>> Now, if I have a String like
>> "some string some content <title>this I want</title>"
>>
>> And I want to use the scan function to extract what is between <title>
>> and </title> how can I build my regular expression. The final result
>> should be:
>> this I want
>>
>> Thnaks
>
> You generaly want to use a HTML parser ... provided that Wuby has one.
>
I wonder what the first hit from googling "ruby html parser" is? Ah
yes, hpricot. A perfectly valid approach.

Personally, in the past I've libtidy'd html to xml and used REXML's
stream parser. This has the rather wonderful benefit of actually being
able to fix some fairly broken html, and failing early if it can't.

> You may be lucky with <title> since it's likely to not include any
> attributes, but still there might be some whitespace INSIDE the tags,
> there may be a comment inside the <title>...</title> that you may or may
> not want, there may be a <title> or </title> inside a comment etc. etc.
> etc.
>
> In (censored) I'd use HTML::Parser from CPAN, but shhhh ... this is a
> Wuby site, we don't speak of such things here.
>
It's a mailing list, not a site... Easy to confuse, possibly, but the
mailing list is the primary interface.

--
Alex

Harry

3/7/2007 3:14:00 PM

0

> I think he meant:
>
> str = "This is <title >what I want</title> and no more"
>
> --
> Alex
>
>

Oh, that's quite different.
Never mind.

Emily Litella :)


--

http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

Jenda Krynicky

3/7/2007 3:49:00 PM

0

Alex Young wrote:
> Jenda Krynicky wrote:
>>> this I want
>>>
>>> Thnaks
>>
>> You generaly want to use a HTML parser ... provided that Wuby has one.
>>
> I wonder what the first hit from googling "ruby html parser" is? Ah
> yes, hpricot. A perfectly valid approach.

Hpricot? How come the name does not surprise me? It's a perfectly clear
name specifying exactly what and how it does.

Jenda
module Enumerable
alias foldl inject # inventing names in a foreign language huh?
end

--
Posted via http://www.ruby-....

J. mp

3/7/2007 3:57:00 PM

0


>>> You generaly want to use a HTML parser ... provided that Wuby has one.
>>>
>> I wonder what the first hit from googling "ruby html parser" is? Ah
>> yes, hpricot. A perfectly valid approach.
>
> Hpricot? How come the name does not surprise me? It's a perfectly clear
> name specifying exactly what and how it does.
>
> Jenda
> module Enumerable
> alias foldl inject # inventing names in a foreign language huh?
> end

Why isn't hpricot a good aproach? any other suggestions?

--
Posted via http://www.ruby-....

Jenda Krynicky

3/7/2007 4:06:00 PM

0

J. mp wrote:
>
>>>> You generaly want to use a HTML parser ... provided that Wuby has one.
>>>>
>>> I wonder what the first hit from googling "ruby html parser" is? Ah
>>> yes, hpricot. A perfectly valid approach.
>>
>> Hpricot? How come the name does not surprise me? It's a perfectly clear
>> name specifying exactly what and how it does.
>>
>> Jenda
>> module Enumerable
>> alias foldl inject # inventing names in a foreign language huh?
>> end
>
> Why isn't hpricot a good aproach? any other suggestions?

No, it most likely is a good approach. It's just that the name is a bit
... wuby. Which is to be expected.

Jenda

--
Posted via http://www.ruby-....