Regular expressions - Again

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-forum.com/.

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"

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-forum.com/\.

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-forum.com/\.

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

···

--

Japanese Ruby List Subjects in English

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 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 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-forum.com/\.

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 :slight_smile:

···

--

Japanese Ruby List Subjects in English

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-forum.com/\.

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-forum.com/\.

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.

Jenda

--
Posted via http://www.ruby-forum.com/\.

Albert Ng wrote:

one does not expect less from the creator of chunky bacon...

can you tell me the whole story? what is the chunky bacon?

···

--
Posted via http://www.ruby-forum.com/\.

J. mp wrote:

Albert Ng wrote:

one does not expect less from the creator of chunky bacon...

can you tell me the whole story? what is the chunky bacon?

http://poignantguide.net/ruby/

Your brain will never be the same again...

···

--
Alex