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

Discussion in 'Perl Misc' started by zhanye815@gmail.com, Jun 21, 2006.

  1. Guest

    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?
     
    , Jun 21, 2006
    #1
    1. Advertising

  2. <> wrote:
    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 21, 2006
    #2
    1. Advertising

  3. wrote:
    > 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.
    See "perldoc -q HTML" for details why and for much better approaches.
    </quote>
    What ever happened to read first, then ask?

    > 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
     
    Jürgen Exner, Jun 21, 2006
    #3
  4. Jürgen Exner <> wrote:
    > wrote:
    >> i want to write a script to convert the html to xhtml,so i want to use
    >> the regular expression


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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 21, 2006
    #4
  5. Re: how to replace this like <img width=100 ...> with <img width="100"...>

    Tad McClellan wrote:
    > Jürgen Exner <> wrote:
    >> wrote:
    >>> i want to write a script to convert the html to xhtml,so i want to use
    >>> the regular expression

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


    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
     
    Mark Clements, Jun 21, 2006
    #5
  6. Xicheng Jia Guest

    wrote:
    > 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
     
    Xicheng Jia, Jun 21, 2006
    #6
  7. Guest

    Tad McClellan wrote:
    > <> wrote:
    > > 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.
    >


    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! :)
     
    , Jun 21, 2006
    #7
  8. Pinocchio <> wrote:
    > Tad McClellan wrote:
    >> <> wrote:
    >> > 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.
    >>

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 22, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    433
    Marina
    Jul 28, 2003
  2. Not4u
    Replies:
    9
    Views:
    1,060
    Not4u
    Feb 27, 2004
  3. cera
    Replies:
    1
    Views:
    1,342
    Neredbojias
    Aug 18, 2007
  4. fred
    Replies:
    3
    Views:
    303
    Zifud
    Mar 17, 2005
  5. Replies:
    5
    Views:
    914
Loading...

Share This Page