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 this I want”

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

Thnaks

On Mar 5, 5:00 pm, “J. mp” [email protected] wrote:

Now, if I have a String like
“some string some content this I want”

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

irb(main):001:0> str = “This is what I
irb(main):002:0” want and no more"
=> “This is what I\nwant and no more”
irb(main):003:0> str[ %r{(.+?)}, 1 ]
=> nil
irb(main):004:0> str[ %r{(.+?)}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 “This is nested</
title> content”, and will result in “This is 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 this I want”

And I want to use the scan function to extract what is between
and 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 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 … that you may or may
not want, there may be a or 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

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

You may be lucky with 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 … that you may or may
not want, there may be a or 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.

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

Jenda

str = “This is what I\n\n\n\n \n want and
no more”
p str

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

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

Jenda K. 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.

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 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 … that you may or
may
not want, there may be a or 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.

Harry wrote:

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

I think he meant:

str = “This is what I want and no more”

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

I think he meant:

str = “This is what I want and no more”


Alex

Oh, that’s quite different.
Never mind.

Emily Litella :slight_smile:

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

Alex Y. wrote:

Jenda K. 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

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

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?

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

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?

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…