Helo !
I've started to learn ruby and I'm amazed with it. Now I have a problem
that I can't solve. If I have a string like this:
"<lyrics artist=XXX album=XXX title=XXX> Lalalalala </lyrics>" how can I
cut the " artist=XXX album=XXX title=XXX" part, so it would look like:
"<lyrcis> Lalalalala </lyrics>" Could you please help me ?
Helo !
I've started to learn ruby and I'm amazed with it. Now I have a problem
that I can't solve. If I have a string like this:
"<lyrics artist=XXX album=XXX title=XXX> Lalalalala </lyrics>" how can I
cut the " artist=XXX album=XXX title=XXX" part, so it would look like:
"<lyrcis> Lalalalala </lyrics>" Could you please help me ?
You can do it with a regular expression like the following, but I must stress that this isn't very robust:
Helo !
I've started to learn ruby and I'm amazed with it. Now I have a problem
that I can't solve. If I have a string like this:
"<lyrics artist=XXX album=XXX title=XXX> Lalalalala </lyrics>" how can I
cut the " artist=XXX album=XXX title=XXX" part, so it would look like:
"<lyrcis> Lalalalala </lyrics>" Could you please help me ?
The very geeky, and most probably least error-prone way would be whacking the
string with a DOM parser, clearing the attributes, and then printing it out
again. Unfortunately, I haven't been doing any DOM manipulation in Ruby, so I
can't provide code.
Helo !
I've started to learn ruby and I'm amazed with it. Now I have a problem
that I can't solve. If I have a string like this:
"<lyrics artist=XXX album=XXX title=XXX> Lalalalala </lyrics>" how can I
cut the " artist=XXX album=XXX title=XXX" part, so it would look like:
"<lyrcis> Lalalalala </lyrics>" Could you please help me ?
Helo !
I've started to learn ruby and I'm amazed with it. Now I have a
problem
that I can't solve. If I have a string like this:
"<lyrics artist=XXX album=XXX title=XXX> Lalalalala </lyrics>" how
can I
cut the " artist=XXX album=XXX title=XXX" part, so it would look like:
"<lyrcis> Lalalalala </lyrics>" Could you please help me ?
You can do it with a regular expression like the following, but I
must stress that this isn't very robust:
Helo !
I've started to learn ruby and I'm amazed with it. Now I have a problem
that I can't solve. If I have a string like this:
"<lyrics artist=XXX album=XXX title=XXX> Lalalalala </lyrics>" how can I
cut the " artist=XXX album=XXX title=XXX" part, so it would look like:
"<lyrcis> Lalalalala </lyrics>" Could you please help me ?
The very geeky, and most probably least error-prone way would be whacking the
string with a DOM parser, clearing the attributes, and then printing it out
again. Unfortunately, I haven't been doing any DOM manipulation in Ruby, so I
can't provide code.
The following is how you do it for valid XML, but the posted example wasn't quite:
I wish I knew how this (/<(\w+)[^>]+>/, "<\\1>")
regular expresion works :).
It reads:
/ < # find a < character
( # capture this next part into $1 (\\1 in the replacement string)
\w+ # followed by one or more word characters
) # end capture
[^>]+ # followed by one or more non > characters
> # and finally a > character
/x
The replacement just restores the <\w+> and leaves out the [^>]+ part (the space and attributes).
Benchmark.bmbm do |x|
x.report("/<(\w+)[^>]+>/") do
tests.times { data.sub(/<(\w+)[^>]+>/, "<\\1>") }
end
x.report("/<(\w+).*?>/") do
tests.times { data.sub(/<(\w+).*?>/, "<\\1>") }
end
end
__END__
James Edward Gray II
···
On Feb 12, 2006, at 12:08 PM, Marcin Mielżyński wrote:
Dňa Nedeľa 12 Február 2006 19:30 James Edward Gray II napísal:
···
On Feb 12, 2006, at 12:08 PM, Marcin Mielżyński wrote:
> James Edward Gray II wrote:
>> >> "<lyrics artist=XXX album=XXX title=XXX> Lalalalala </
>>
>> >".sub(/<(\w+)[^>]+>/, "<\\1>")
>> => "<lyrics> Lalalalala </lyrics>"
>
> reluctant would a bit faster:
>
> p "<lyrics artist=XXX album=XXX title=XXX> Lalalalala </
> >".gsub(/<(\w+).*?>/, "<\\1>")
Benchmark.bmbm do |x|
x.report("/<(\w+)[^>]+>/") do
tests.times { data.sub(/<(\w+)[^>]+>/, "<\\1>") }
end
x.report("/<(\w+).*?>/") do
tests.times { data.sub(/<(\w+).*?>/, "<\\1>") }
end
end
__END__
James Edward Gray II
The nongreedy match has to "back up" and retry on every character after the
tag name, whileas James' [^>] doesn't ever have to back up. In fact, even a
greedy .* would probably be faster than a nongreedy one in this case.
Gotta love the black art that is optimizing regexps.
The nongreedy match has to "back up" and retry on every character after the tag name, whileas James' [^>] doesn't ever have to back up. In fact, even a greedy .* would probably be faster than a nongreedy one in this case.
Gotta love the black art that is optimizing regexps.
Ooops.. You are right!
But as I read greedy quantifiers do backtrack as well (but not in the case above).
/a+aa/ =~ "aaaaa"
will backtrack two characters
only possesive quantifier (in oniguruma e.g.) consumes in the real, greedy way.
Dňa Nedeľa 12 Február 2006 21:38 Marcin Mielżyński napísal:
David Vallner wrote:
> The nongreedy match has to "back up" and retry on every character after
> the tag name, whileas James' [^>] doesn't ever have to back up. In fact,
> even a greedy .* would probably be faster than a nongreedy one in this
> case.
>
> Gotta love the black art that is optimizing regexps.
Ooops.. You are right!
But as I read greedy quantifiers do backtrack as well (but not in the
case above).
/a+aa/ =~ "aaaaa"
will backtrack two characters
only possesive quantifier (in oniguruma e.g.) consumes in the real,
greedy way.
so
/a++aa/ =~ "aaaaa"
won't match.
lopex
Yes, they do backtrack. The point is in using the one that you expect to
backtrack less.
Since in this case we very well knew there's going to be quite a few
characters after the first word, the nongreedy quantifier was slower.
Where the REAL black magic is whether a greedy or possessive quantification of
the [^>] variant would be faster. *snicker* Anyone running 1.9 able to BM
this?