how to replace this like <img width=100 ...> with <img width="100" ...>

Z

zhanye815

i want to write a script to convert the html to xhtml,so i want to use
the regular expression to solve some problem like <img width=100
height=100...> converting into <img width="100" height="100"...>.how to
do it?
 
T

Tad McClellan

i want to write a script to convert the html to xhtml,so i want to use
the regular expression to solve some problem like <img width=100
height=100...> converting into <img width="100" height="100"...>.how to
do it?


I know how to do that.

But it does not meet your requirement of using a regex so, never mind.
 
J

Jürgen Exner

i want to write a script to convert the html to xhtml,so i want to use
the regular expression

Now, were is that posting, I know it must be somewhere...
Ah, there it is, just from this morning:
<quote>
As has been mentioned gazillion of times REs are the wrong tool for parsing
HTML.
to solve some problem like <img width=100
height=100...> converting into <img width="100" height="100"...>.how
to do it?

Simple. By not using the wrong tool but a real ready-made HTML parser from
CPAN.
AFAIR there was even a sample program in the HTML::parser package that did
something very similar to what you are asking for.
jue
 
T

Tad McClellan

What ever happened to read first, then ask?
AFAIR there was even a sample program in the HTML::parser package that did
something very similar to what you are asking for.


Searching CPAN, or even Google, for "html2xhtml" finds stuff.

I guess we must then guess that this OP did not even look...
 
M

Mark Clements

Tad said:
Searching CPAN, or even Google, for "html2xhtml" finds stuff.

I guess we must then guess that this OP did not even look...

But that's the trick, isn't it? Knowing exactly what to look for only
comes from experience. The "x2y" idiom is (I'd argue) a unixism. It's a
little unfair to say "why didn't you search for 'xyz123'? simple" when
it isn't at all obvious, unless one is vastly experienced, that one
needed to search for 'xyz123'. This is particularly the case with perldoc -q

eg

F:\Documents and Settings\Mark>perldoc -q html
Found in F:\Perl\lib\pod\perlfaq9.pod
How do I remove HTML from a string?

If I were the OP and had read this, am not sure I'd have read that
particular item any further.

I'd agree, though, that the OP doesn't appear to have done much research
of his own here.

Mark
 
X

Xicheng Jia

i want to write a script to convert the html to xhtml,so i want to use
the regular expression to solve some problem like <img width=100
height=100...> converting into <img width="100" height="100"...>.how to
do it?

Although it should be more reliable to handle your problem with some
CPAN modules. But trying it by yourself doesnt hurt. you can learn
something from either way. here is a trail solution for you:
#################
use strict;
use warnings;

local $/ = undef;
my $paren = qr/"[^"]*"|'[^']*'/;
while (my $line = <ARGV>) {
$line =~ s{($paren)|(<(?:$paren|[^>"'])*>)}{$1 or func($2)}ge;
print $line;
}

sub func {
my $x = shift;
$x =~ s/(\w+)\s*=\s*([^'"\s>]+)/$1="$2"/g;
$x;
}
##################
if you need to consider the escaped bouble-quotes or apostrophes, like
"he say\"hello!\".", then you may want to change "[^"]" in $paren to
some more complex form, like:

"[^"\\]*(?:\\.[^"\\]*)*"

similar to the other part.

Good luck
Xicheng
 
K

krakle

Tad said:
I know how to do that.

But it does not meet your requirement of using a regex so, never mind.

It isn't uncommon in the Computer Programming world to encounter people
who aren't good in social situations... Tad you are the prime example
of this... Very knowledgable guy but have no idea how to interact with
others in social conversations... It's not that you are rude... It's
just that you don't get out often so you are socially inexperienced..
There are others like you too... Geeks are geeks... They aren't exactly
known for their great social skills...

Let's go out and have a few beers buds! :)
 
T

Tad McClellan

Pinocchio said:
It isn't uncommon in the Computer Programming world to encounter people
who aren't good in social situations...


That is true enough.

Tad you are the prime example
of this...


That's rich, considering the vileness of your demonstrated conduct
in this newsgroup's social situation.

Very knowledgable guy but have no idea how to interact with
others in social conversations... It's not that you are rude...


It is not that I am rude, it is that I am *teaching*.

I could have launched an explanation of why XY problems are bad,
or I could arrange for the OP to learn that for himself.

I understand human cognition enough to try the approach that is
most likely to stick, saving the OP from falling prey to it in
the future.

Besides, I fully expected the OP to come back asking for the
non-regex solution, so I'd have the opportunity to answer the
question then.

It's
just that you don't get out often so you are socially inexperienced..


You do not know me, so that will just make your nose get longer.

Let's go out and have a few beers buds! :)


We are not your buds.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top