Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found