Go Back   Velocity Reviews > Newsgroups > PERL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

PERL - A little regex help?

 
Thread Tools Search this Thread
Old 07-03-2003, 06:08 PM   #1
Default A little regex help?


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.

--
i.m.
All views, opinions and alleged facts expressed by this tactless moron are
protected by the constitution of the United States of America and should be
taken as good natured and friendly unless specifically stated otherwise.



Ivan Marsh
  Reply With Quote
Old 07-04-2003, 07:00 PM   #2
JamesW
 
Posts: n/a
Default Re: A little regex help?

$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



"Ivan Marsh" <> wrote in message news:<>...
> 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.

  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump