using regexes as command line parameters

Discussion in 'Perl Misc' started by JMI, Nov 18, 2004.

  1. JMI

    JMI Guest

    Hello

    Short question, but maybe somebody knows how-to

    Let's imagine your program accepts two command line arguments to perform
    replaces in a string:
    usage: myprogram -s "foo" -r "bar"
    where s is the search regex and r the replace regex.

    Everything works fine, even grouping with parenthesis, etc. As described in
    detail in the cookbook.

    But:
    When I want to use a captured value in the replacement regex, it does
    simply not work. Using all possible syntaxes ( as \$1, or \\$1...) only
    leads to either an exmpty value in the replaced string, or a plain-text
    "$1" text when too much is escaped.

    The code looks like:

    my $rx;
    if ($case_sensitive) {
    $rx = qr/$search_pattern/;
    } else {
    $rx = qr/$search_pattern/i;
    }
    print "Patching file:\t$origin\n" if ($debug_mode ||
    $verbose_mode);
    my $myfile = '';

    print "Opening $origin\n" if $debug_mode;
    open(OLD, "< $origin")
    while (<OLD>) {
    $myfile .= $_;
    }
    print "Closing $origin\n" if $debug_mode;
    close(OLD)

    open(NEW, "> $playground")
    print "Replacing $search_pattern with $replace_pattern\n" if
    $debug_mode;
    $myfile =~ s/$rx/$replace_pattern/gs;
    print NEW $myfile
    close(NEW)

    Hope it's the right place to find some people having had the same
    problem,....

    thanks and best regards
    JM
    JMI, Nov 18, 2004
    #1
    1. Advertising

  2. JMI

    Paul Lalli Guest

    "JMI" <waggis_at_hotmail.com> wrote in message
    news:Xns95A5B40F63810waggishotmailcom@192.37.1.74...
    > Let's imagine your program accepts two command line arguments to

    perform
    > replaces in a string:
    > usage: myprogram -s "foo" -r "bar"
    > where s is the search regex and r the replace regex.
    >
    > Everything works fine, even grouping with parenthesis, etc. As

    described in
    > detail in the cookbook.
    >
    > But:
    > When I want to use a captured value in the replacement regex, it does
    > simply not work. Using all possible syntaxes ( as \$1, or \\$1...)

    only
    > leads to either an exmpty value in the replaced string, or a

    plain-text
    > "$1" text when too much is escaped.


    Giving simply $1 makes the shell pass the shell's own $1 variable to
    your script.
    Giving \$1 passes the literal string '$1' to your script.

    What you need to do is tell Perl to evaluate that literal string as
    Perl:

    perl -le'$_ = q/foo/; s/($ARGV[0])/$ARGV[1]/ee; print;' foo \$1

    This is a trivial example, of course, but it does illustrate that perl
    is correctly replacing 'foo' with the captured match (which also happens
    to be 'foo').

    For more info, look up the /e modifier in
    perldoc perlretut
    or
    perldoc perlre

    Paul Lalli
    Paul Lalli, Nov 18, 2004
    #2
    1. Advertising

  3. JMI

    JMI Guest

    Worked ! Thanks a lot :)))


    "Paul Lalli" <> wrote in
    news:2Q4nd.12483$wY2.3433@trndny05:

    > "JMI" <waggis_at_hotmail.com> wrote in message
    > news:Xns95A5B40F63810waggishotmailcom@192.37.1.74...
    >> Let's imagine your program accepts two command line arguments to

    > perform
    >> replaces in a string:
    >> usage: myprogram -s "foo" -r "bar"
    >> where s is the search regex and r the replace regex.
    >>
    >> Everything works fine, even grouping with parenthesis, etc. As

    > described in
    >> detail in the cookbook.
    >>
    >> But:
    >> When I want to use a captured value in the replacement regex, it does
    >> simply not work. Using all possible syntaxes ( as \$1, or \\$1...)

    > only
    >> leads to either an exmpty value in the replaced string, or a

    > plain-text
    >> "$1" text when too much is escaped.

    >
    > Giving simply $1 makes the shell pass the shell's own $1 variable to
    > your script.
    > Giving \$1 passes the literal string '$1' to your script.
    >
    > What you need to do is tell Perl to evaluate that literal string as
    > Perl:
    >
    > perl -le'$_ = q/foo/; s/($ARGV[0])/$ARGV[1]/ee; print;' foo \$1
    >
    > This is a trivial example, of course, but it does illustrate that perl
    > is correctly replacing 'foo' with the captured match (which also happens
    > to be 'foo').
    >
    > For more info, look up the /e modifier in
    > perldoc perlretut
    > or
    > perldoc perlre
    >
    > Paul Lalli
    >
    JMI, Nov 22, 2004
    #3
    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. Jonck van der Kogel
    Replies:
    2
    Views:
    964
    Jonck van der Kogel
    May 27, 2004
  2. Replies:
    0
    Views:
    507
  3. Roedy Green

    Regexes on the command line

    Roedy Green, Feb 26, 2009, in forum: Java
    Replies:
    12
    Views:
    506
    Martin Gregorie
    Mar 2, 2009
  4. soldier.coder

    Parsing HTML using regexes and arrays.

    soldier.coder, Nov 7, 2008, in forum: Ruby
    Replies:
    1
    Views:
    92
    Michael Libby
    Nov 7, 2008
  5. Victor Hooi
    Replies:
    6
    Views:
    163
    Chris Angelico
    Dec 13, 2012
Loading...

Share This Page