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
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</
···
On Mar 5, 5:00 pm, "J. mp" <joaomiguel.pere...@gmail.com> wrote:
content</title>", and will result in "This is <title>nested"
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.
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.
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.
one does not expect less from the creator of chunky bacon...
···
On 3/7/07, Jenda Krynicky <jenda@cpan.org> wrote:
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.