Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://22213]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-23 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found