[ILUG] Regex Question

John Allen john.allen at dublinux.net
Mon Sep 27 16:47:14 IST 2004


On Monday 27 September 2004 15:19, Gareth Eason wrote:
> 	There is probably a better way, but I would crack out the perl on this
> one.
>

Or
# perl -pi -e 's|<div class="foo"></div>|<div class="foo">TEST</div>|g' foobar.html

> 	open( FILE, "foobar.html" ) or die;
> 	open( OUTFILE, ">output.html" ) or die;
> 	my $counter = 0;
>
> 	while(<FILE>)
> 	{
> 		if( /<div class="foo"><\/div>/ )
> 		{
> 			$counter++;
> 			if( $counter == 3 )
> 			{
> 				$counter = 0;
> 				s/<div class="foo"><\/div>/<div class="foo">TEST<\/div>/;
> 			}
> 		}
> 		print OUTFILE $_;
> 	}
>
>
> 	Jobsa... (Usual disclaimers, typos, etc. apply, YMMV.)
>
> 	Best regards,
> 	-->Gar
>
> Rory Winston wrote:
> > Regex gurus,
> >
> > I'm trying to do a simple substitute in Vim. I hav a HTML page with
> > hundreds of lines like:
> >
> > <div class="foo"></div>
> > <div class="foo"></div>
> > <div class="foo"></div>
> > <div class="foo"></div>
> >
> > What I would like to be able to do is replace every n'th element with
> > something else. So if n = 3, then the result is:
> >
> > <div class="foo"></div>
> > <div class="foo"></div>
> > <div class="foo">TEST</div>
> > <div class="foo"></div>
> > <div class="foo"></div>
> > <div class="foo">TEST</div>
> >
> > Does anybody know how I might achieve this?
> >
> > Thanks in advance,
> > Cheers,
> >
> > Rory



More information about the ILUG mailing list