[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sheflug] quick Reg exp exercise



On Tue, Jun 29, 2004 at 08:12:13PM +0100, Chris J wrote:
> There's three substitutions:
> 	- The first matches <ELEMENT ...>           i.e., up to the space.
> 	- The second matches <ELEMENT>              no attributes
> 	- The third matches </ELEMENT>              closing attributes
> 
> 
> #!/usr/bin/perl
> 
> while (<>) {
>    s/<([A-Za-z0-9]*) /"<" . lc($1) . " "/ge;
>    s/<([A-Za-z0-9]*)>/"<" . lc($1) . ">"/ge;
>    s/<\/([A-Za-z0-9]*)>/"<\/" . lc($1) . ">"/ge;
>    print;
> }
> ## End of script
> 
> Save, chmod, and call as:
> 	/path/to/script.pl < input.html > output.html
> 
> Caveats: This is the first perl I've written in about a year, so there may 
> be an even more elegant solution. Secondly, I don't know if I've caught all 
> the cases when an element needs to be caught (e.g., if the space is a tab, 
> then that element won't change - but that's a simple thing to rectify and 
> left as an exercise for the reader, ditto another to catch the "/>" 
> terminator in XML).


    while (<>) {
	s{ (< /?) (\w+) \b }{ $1 . lc($2) }gxe
    }

Or to do it as a one-liner to edit a list of files *.html in-place,

    perl -i -pe 's{ (< /?) (\w+) \b }{ $1 . lc($2) }gxe' *.html

-i says edit each file in-place,
-p says wrap the code with an implicit while (<>) { ...; print }

-- 
My get-up-and-go just got up and went.
___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

  GNU the choice of a complete generation.