I have a file (called in.txt) that is formatted with field demiliters and text like this: BEGTITLE blah,blah...blah ENDTITLE. The title field is followed by other fields delimiteted the same way. I have been assigned the glorious task of replacing specific words or useless metacharachters only between the title tags, leaving all other instances untouched. I have tried various methods with zero luck (this will probably not be surprising to an experienced PERL programmer!). Here is a sample of my code and INFILE:
Sample in.txt BEGPUB Wirey Haired Dog's Life ENDPUB BEGTITLE My dog has wirey hair ENDTITLE BEGTXT My wirey hair dog...blah, blah. ENDTXT
My Script
#!/usr/bin/perl -w open(INFILE,$ARGV[0]) or die "INFILE CROAKED"; open(OUTFILE,">$ARGV[1]) or die "OUTFILE CROAKED"; While(<INFILE>){ while(/BEGTITLE.*wirey.*?ENDTITLE/){ $_=~s/wirey/smooth/i; print OUTFILE $_;} } close INFILE; close OUTFILE;
I want to replace wirey with smooth in just the title field (which may span multiple lines). My example will replace the word wirey in the other fields as well. I have no basis to believe that my code is anywhere near being correct or useful. I do not even know why I though this could lead anywhere good. Any ideas or suggestions will be much appreciated. Thanks in advance, Stephen

In reply to Regex: find/Replace words between tags by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.