A little regex help?

Discussion in 'Perl' started by Ivan Marsh, Jul 3, 2003.

  1. Ivan Marsh

    Ivan Marsh Guest

    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, Jul 3, 2003
    #1
    1. Advertising

  2. Ivan Marsh

    JamesW Guest

    $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.
     
    JamesW, Jul 4, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThaDoctor
    Replies:
    3
    Views:
    409
    Alan Woodland
    Sep 28, 2007
  2. Replies:
    3
    Views:
    822
    Reedick, Andrew
    Jul 1, 2008
  3. Replies:
    0
    Views:
    155
  4. Ivan Marsh

    A little regex help?

    Ivan Marsh, Jul 3, 2003, in forum: Perl Misc
    Replies:
    10
    Views:
    153
    JamesW
    Jul 4, 2003
  5. Daniel
    Replies:
    1
    Views:
    228
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page