From: David Vallner Date: 2006-12-03T03:15:54+09:00 Subject: Re: search and replace --------------enigAF59EE22A0069F24937D8CA9 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable ishamid wrote: > [total novice here] >=20 > Hi, >=20 > I have a series of expressions like this (shortened from verbose xml) > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > [1 > [2 > [3 > [4 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > I want to globally replace each such line with just >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > \head > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > followed by a line space so I get >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > \head >=20 > \head >=20 > \head >=20 > \head > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > etc. >=20 > I am modifying a script with lines like >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > data.gsub!(/.*?<(office:text).*?>(.*?)<\/\1>.*/mois) do > '\starttext' + "\n" + $2 + "\n" + '\stoptext' > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > and don't yet know enough to completely understand. Probably a few more= > hours/days of study will get me there but I need this urgently so... >=20 > THNX in advance >=20 Urght. *ducks* > Best > Idris >=20 >=20 Regexps and XML always tend to blow up for me. The pattern you're searching for seems to be a complete element, why not use and XPath? With REXML, it should be something like: document.elements.each('//text:sequence') {|sequence| sequence.replace_with(REXML::Text.new("\\head\n", true))} Substitute the XPath expression with one of desired precision. I'm a little unsure around how REXML treats namespaces in XPath and such, but if you know what prefix will be used in the document, that should work ou= t. The script might also require a little more massaging if you're outputting to plaintext, but treating XML like, well, XML might get the heavy lifting of searching for patterns in it done faster if you use a pattern language operating on the DOM structure directly. David Vallner --------------enigAF59EE22A0069F24937D8CA9 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFccLUy6MhrS8astoRAslhAJ45yqsdcH9HGm/SvN2MtjAV75M1VgCeN0Tx UXiQ39eCXVG32nDKEQZjODI= =vyHu -----END PGP SIGNATURE----- --------------enigAF59EE22A0069F24937D8CA9--