So - so far all of the other responses have assumed you can read a line at a time. Sometimes you can't. Well - here is one possible option that will let you do what you want (assuming I know what you want) on a single string.
my $str = "<html>
<title>Hi</title>
<body>
Some other lines
of stuff
</body>
</html>
";
$str =~ s{ ^(.+)$
(??{
lc(substr $^N, 0, 7) eq "<title>"
? '(?!)'
: '(?=)'
})
}{<para>$1</para>}xgm;
print $str;
# prints
<para><html></para>
<title>Hi</title>
<para><body></para>
<para>Some other lines</para>
<para>of stuff</para>
<para></body></para>
<para></html></para>
This uses the delayed regex feature that lets you put a regex in a later point. It takes whatever is in $^N (the last match) and sees if begins with
<title>. If it begins with title we put in '(?!)' which is a negative look ahead that will always fail - otherwise we put in '(?=)' which is a positive look ahead that will always succeed.
Unfortunately we have to do the testing to see if each line begins with title with with string methods rather than a nested regex. If we used a regex inside the (??{}) we would confuse the regex engine and in many (if not all) cases cause a segfault. But string methods often work just fine.
my @a=qw(random brilliant braindead); print $a[rand(@a)];
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.