Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to substitute all the words before a certain word in the middle of a line?

by Ted Nitz (Chaplain)
on Jul 12, 2000 at 20:01 UTC ( #22213=note: print w/ replies, xml ) Need Help??


in reply to How to substitute all the words before a certain word in the middle of a line?

I'm assuming that there should have been line breaks in the sample data, so that each sentince was on a separate line. Also, this code will match everything before the first occurance of the trigger word (nice in this case). You can replace <DATA> with another filehandle or with <> depending upon where you want to get the lines.
my $triggerword = "nice"; my $replacement = "A "; while(<DATA>){ s/^.*?(?=$triggerword)/$replacement/o; print; } __DATA__ This is a nice weather. He is a nice guy. We have some nice food in our dinner.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://22213]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2016-06-25 03:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (323 votes). Check out past polls.