http://qs1969.pair.com?node_id=290563


in reply to Regexp Problem with greedy

A fundamental problem with this script is that it is chopping a file at each \n. If a tagged text spans more than one line, you won't catch it. Do you want to catch the <M> tags only if its ending tag has digits?

This will do it. (I'll leave it up to you to improve it so that it can detect tagged text that spans over line breaks.)

#!/usr/local/bin/perl -w while (<DATA>) { »· for (m{<M>([^<]+)(?=</M\d+>)}ig) { print $_,"\n"}; } __DATA__ America's first <M>swept-wing</M>, <M>multiengine jet</M> <M>bomber</M +> was the <M>B-47 Stratojet</M200>, and the first <M>swept-wing fighter</M> was the <M>F-86 Sabre Jet</M201>. Both used new swept-wing data found in Germany after <M>World War II</M> and sent back to the United States by American scientists. This photograph, from <D>1951</D +>, was taken the first time the two flew together over <PL>Kansas</PL>.· <M>Bogus</M2> <M>airplane</M3>
But you really should read perlre and if you get a chance, by all means read Jeff Friedl's excellent Mastering Regular Expressions (ISBN 0596002890). And of course, be wary of Dot star! (Death to Dot Star!).