From: William James Date: 2006-07-24T13:20:06+09:00 Subject: Re: strip tags? Christian Neukirchen wrote: > "Andreas S." writes: > > > Daniel Baird wrote: > >> On 7/23/06, Stefan Scholl wrote: > >>> > >>> Max Benjamin wrote: > >>> > Is there an easy way to strip html tags from strings? > >>> > >>> A regex isn't always the _best_ way to deal with markup > >>> languages, but for an _easy_ way it's good enough. > >> > >> > >> the problem is, it's not always the _correct_ way. > >> > >>
> > > > This is no correct HTML, < and > have to be encoded as entities. > > It's valid XHTML: > > $ echo '' | xmllint - > > > > However, '<' needs to be escaped: > > $ echo '' | xmllint - > -:1: parser error : Unescaped '<' not allowed in attributes values > > > -- > Christian Neukirchen http://chneukirchen.org re = %r{ < (?: # Any characters but > or " . [^>"] + | # Characters within quotes. # Allow escaped quotes. " (?: # Accept any escaped character. \\. | [^"\\] + ) * " ) * > }xm print DATA.read.gsub( re, '' ) __END__ Some<><""> bar" /> text to save for " />reading.