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!).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.