Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Write to existing file with character insert

by Don Coyote (Monk)
on Oct 24, 2012 at 12:39 UTC ( #1000624=note: print w/ replies, xml ) Need Help??


in reply to Write to existing file with character insert

Hi, you want to use a substitution regex operator. the match operator is described as m//, this is the operator you have used. The substition operator is described s///.

The pattern you wish to match goes in the first section as per a normal match, the pattern you wish to replace in the second section. You can use parentheses in the first section to capture the matched pattern and reuse it in the second section within the special variables $1 through $9

if ($_ =~ /start ux/){ print "#$_"; }

(assuming the whole line is a pattern match) becomes...

s/(pattern)/#$1/;

if you wanted to insert an octothorpe if the pattern matches anywhere in the line, you could have your regex match the whole line if part of it matches...

s/^(.*(pattern).*)$/#$1/;

regex operates on the $_ variable, and substitution matching is an 'if' operation in itself. In this case the parentheses match in subsequential order from first open through to second open (pattern matches $2). The outer characters say, anything matches.


s/(Coy)/$1ote/;


Comment on Re: Write to existing file with character insert
Select or Download Code
Re^2: Write to existing file with character insert
by Anonymous Monk on Oct 24, 2012 at 12:43 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-10-22 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls