A little regex help?

I

Ivan Marsh

Hey Folks,

This isn't actually a perl question but since you folks ar the regex
experts I thought you might be able to help.

I want to do a search and replace in my text editor (that supports regex)
for any line that begins with print( and ends with "); and replace the
ending "); with \n"); where the line doesn't alredy end in \n");

ex:

print("foo");

becomes:

print("foo\n");

Thanks.
 
J

JamesW

$line='print ("foo")';

$line=~s/^(print #anchor to beginning of string
.* #match any text
\)) #match closing parenthesis (need to escape
character)
/$1\n/x; #having wrapped match in () can use memory
variable
#and append newline.

print $line; #print ("foo")\n

Ivan, not sure of the 'flavour' of your regex machine so will need to
look up how to use backreferences. Probably \1. The x modifier at
the end is just so I can comment the code for explanations.

s/^(print.*\))/$1\n/;

hth
j
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top