Re: Regex replacement via external command

Discussion in 'Perl Misc' started by Charles DeRykus, Jan 23, 2014.

  1. On 1/22/2014 6:31 AM, * Tong * wrote:
    > On Wed, 22 Jan 2014 06:25:18 +0000, Tim McDaniel wrote:
    >
    >> Don't do `...` when there may be a lot of output. Please see the
    >> perlopentut man page, specifically "pipe open", and don't try to pass
    >> long values on the command line.

    >
    > The problem I was dealing with is, I need to pick out a big chunk of
    > input string (>200K, by regex), feed it to external program (which is
    > pipe after pipe after pipe), then replace the matching string with the
    > processed result. what's the proper way to do it (for big matching
    > chunks)?
    >
    > A thousand time over-simplified version is:
    >
    > perl -e 'print("aa". "x" x 238565 . "bb", "\n")' > HttpBody
    > <HttpBody perl -n000e 's,(x+),`echo $1 | wc -c`,eg; print'
    >
    > The problem is that I not only need to process this big chunk of
    > matching string via the external program, but I also need to replace the
    > matching string with the result of the external process. Putting two
    > together is where the problem for me.
    >


    I'd recommend putting the mule train on a separate line:

    if ( ($pre, $match, $post) = $huge_str =~ /...pattern.../p ) {
    my $new_match = ... # external process | ... | ...
    say $pre, $new_match, $post;
    }

    --
    Charles DeRykus
     
    Charles DeRykus, Jan 23, 2014
    #1
    1. Advertising

  2. On 1/22/2014 6:47 PM, Charles DeRykus wrote:
    > ...
    >
    > if ( ($pre, $match, $post) = $huge_str =~ /...pattern.../p ) {
    > my $new_match = ... # external process | ... | ...
    > say $pre, $new_match, $post;
    > }
    >



    Make that:

    if ( $huge_str =~ /...pattern.../p ) {
    (my $new_match = ${^MATCH}) = ext process ... | ...
    say ${^PREMATCH}, $new_match, ${^POSTMATCH};
    }

    --
    Charles DeRykus
     
    Charles DeRykus, Jan 23, 2014
    #2
    1. Advertising

  3. On 1/22/2014 7:47 PM, Charles DeRykus wrote:
    > On 1/22/2014 6:47 PM, Charles DeRykus wrote:
    >> ...
    >>
    >> if ( ($pre, $match, $post) = $huge_str =~ /...pattern.../p ) {
    >> my $new_match = ... # external process | ... | ...
    >> say $pre, $new_match, $post;
    >> }
    >>

    >
    >
    > Make that:
    >
    > if ( $huge_str =~ /...pattern.../p ) {
    > (my $new_match = ${^MATCH}) = ext process ... | ...

    ^^^^^^^^^^^^^^^^^^^^^^^^^
    my $match = ${^MATCH};
    my $new_match = ext process .... $match | ... ;


    --
    Charles DeRykus
     
    Charles DeRykus, Jan 23, 2014
    #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. Replies:
    3
    Views:
    823
    Reedick, Andrew
    Jul 1, 2008
  2. Anthony Roy
    Replies:
    7
    Views:
    224
    A. Sinan Unur
    Mar 4, 2005
  3. Rainer Weikusat

    Re: Regex replacement via external command

    Rainer Weikusat, Jan 22, 2014, in forum: Perl Misc
    Replies:
    2
    Views:
    99
    Rainer Weikusat
    Jan 23, 2014
  4. George Mpouras

    Re: Regex replacement via external command

    George Mpouras, Jan 22, 2014, in forum: Perl Misc
    Replies:
    0
    Views:
    105
    George Mpouras
    Jan 22, 2014
  5. Rainer Weikusat

    Re: Regex replacement via external command

    Rainer Weikusat, Jan 23, 2014, in forum: Perl Misc
    Replies:
    1
    Views:
    86
    Rainer Weikusat
    Jan 23, 2014
Loading...

Share This Page