develooper Front page | perl.beginners | Postings from January 2002

Re: regexp to replace double with single quotes inside html tags

Thread Previous | Thread Next
From:
birgit kellner
Date:
January 6, 2002 04:37
Subject:
Re: regexp to replace double with single quotes inside html tags
Message ID:
314415926.1010323980@[10.0.0.140]
--On Montag, 31. Dezember 2001 01:57 -0800 "John W. Krahn" <krahnj@acm.org> 
wrote:

> Here is the verbose version.
>
> $string =~ s{            # start regular expression
>               (          # start capture
>                 <        # match a less-than symbol
>                 [^>]+    # match one or more characters not a
> greater-than symbol                 >        # match a greater-than symbol
>               )          # end capture
>             }
>             {            # start replacement expression
>               $a = $1;   # can't modify $1 so copy it
>               $a =~ tr|"|'|;  # replace double quotes with single quotes
>               $a         # use this new string to replace the old one
>             }gex;        # g = global pattern match
>                          # e = evaluate the replacement expression
>                          # x = allow whitespace and comments
>

Yes, the decisive point - if I understand it correctly - is that it 
performs the translation only within $1, i.e. within the match for text 
surrounded by pointed brackets.

Many thanks for the code, by the way. I think I can use it, even though 
there remains one case where it doesn't work:

<img src="http://someimagefile.jpg" alt="A > B">


That is, cases where tag attributes themselves contain pointed brackets 
(which is, AFAIK, legal).

Birgit Kellner

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About