Replacing text - problem with $1

Discussion in 'Perl Misc' started by Martin Kristensen, Sep 3, 2003.

  1. I'm trying to replace some text in some txt-files with a perl call
    like this...

    perl -pi -e "s/brad(.+?)bill/brian$1bob/gm;" *.txt

    This should replace "brad123bill" by "brian123bob" - but instead I
    just get "brianbob". What's wrong? Probably something with the $1
    variable, but what?

    Regards
    Martin
    Martin Kristensen, Sep 3, 2003
    #1
    1. Advertising

  2. Martin Kristensen

    Anno Siegel Guest

    Martin Kristensen <> wrote in comp.lang.perl.misc:
    > I'm trying to replace some text in some txt-files with a perl call
    > like this...
    >
    > perl -pi -e "s/brad(.+?)bill/brian$1bob/gm;" *.txt
    >
    > This should replace "brad123bill" by "brian123bob" - but instead I
    > just get "brianbob". What's wrong? Probably something with the $1
    > variable, but what?


    Nothing is wrong with the regex, except for the /m modifier, which does
    nothing.

    Anno
    Anno Siegel, Sep 3, 2003
    #2
    1. Advertising

  3. -berlin.de (Anno Siegel) wrote in message news:<bj4d6l$ntk$-Berlin.DE>...
    > Martin Kristensen <> wrote in comp.lang.perl.misc:
    > > I'm trying to replace some text in some txt-files with a perl call
    > > like this...
    > >
    > > perl -pi -e "s/brad(.+?)bill/brian$1bob/gm;" *.txt
    > >
    > > This should replace "brad123bill" by "brian123bob" - but instead I
    > > just get "brianbob". What's wrong? Probably something with the $1
    > > variable, but what?

    >
    > Nothing is wrong with the regex, except for the /m modifier, which does
    > nothing.


    I solved the problem by replacing " by ' so now it works perfectly for
    some reason. :)

    Martin
    Martin Kristensen, Sep 3, 2003
    #3
  4. (Martin Kristensen) wrote in message news:<>...
    > I'm trying to replace some text in some txt-files with a perl call
    > like this...
    >
    > perl -pi -e "s/brad(.+?)bill/brian$1bob/gm;" *.txt
    >
    > This should replace "brad123bill" by "brian123bob" - but instead I
    > just get "brianbob". What's wrong? Probably something with the $1
    > variable, but what?


    Regexp is fine. By the time it gets to perl, though, the s command
    looks like this:

    s/brad(.+?)bill/brianbob/gm

    Your shell has replaced $1 with its value (nothing) before passing it
    to perl. Replacing the double quotes with single quotes should work.
    If not, time to dig out the shell's documentation.

    Hunter
    --
    http://www.hunterandlori.com
    Hunter Johnson, Sep 3, 2003
    #4
    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. Brown Smith
    Replies:
    1
    Views:
    494
    Frankie
    Jun 25, 2005
  2. OB
    Replies:
    1
    Views:
    394
    Steve C. Orr [MVP, MCSD]
    Dec 2, 2005
  3. Tim_Mac
    Replies:
    2
    Views:
    595
    Tim_Mac
    Jan 21, 2006
  4. Chris
    Replies:
    5
    Views:
    8,032
    Christopher R. Barry
    Oct 11, 2003
  5. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    262
    Chris Hohmann
    Apr 11, 2005
Loading...

Share This Page