chintak86 has asked for the wisdom of the Perl Monks concerning the following question:

Could anyone help with replacing the following string:

test("test with the following: test("GC_test

using perl regex. I had been trying something as follows with no luck:

s/(test\)"test)/(test\)GC_"test)/g

Please help, going crazy :(

Formatting added by davido

Replies are listed 'Best First'.
Re: Replace strings with "("
by Frantz (Monk) on Jan 29, 2005 at 23:43 UTC
    See perldoc perlre :)
    my $string = 'test("test ... bla bla bla")'; $string =~ s/test\("test/test\("GC_test/;
      That was the first thing I tried. The problem with that was, the strings weren't modified. It remained test("test.
      I don't know if this info is relevant, but I will share with you if it helps. I am using active perl on windows 2000.
      The regex I tried to use came from another site on active perl. I can't remember which site it was from though. There it was mentioned to put the paren inside a open and close paren and quote the paren in the string. i.e. (\()
      By the way I did try your solution just in case. But I still have my initial result. Any help would be appreciated. Thanks
        Are you reading these in from a file and seeing that their not changed in the file ?

        If so, try this recipe for in-place editing

        prompt> perl -i -pe 's/"t/"GC_t/' file_to_be_changed.txt

        The -i flag tells perl to make changes (like regex substitutions) in the original file, dont make a backup

        The -pe is two options joined together - -p is print every line in the supplied file back out to itself (thats what -i and -p together do - print every line back to the same file)

        The -e tells perl to apply the regex to every line in the file - and hence the "t lines become "GC_t before being printed back out.

        You can find more options like these in "perldoc perlrun", or Perl Command-Line Options.

        Cheers

        ...it is better to be approximately right than precisely wrong. - Warren Buffet

        with perl 5.8.4 on linux(debian unstable), the following code
        1 #!/usr/bin/perl 2 3 use strict; 4 5 my $string = 'test("test ... bla bla bla")'; 6 $string =~ s/test\("test/test\("GC_test/; 7 8 print $string."\n";
        return test("GC_test ... bla bla bla")

        perhaps the probleme is your verion of perl
Re: Replace strings with "("
by jkeenan1 (Deacon) on Jan 29, 2005 at 23:51 UTC
    I'm not exactly clear what your objective is, i.e., what in the original string is to be replaced and what is to be preserved.

    But assuming that an open paren is to be preserved, then you have to backslash it. Instead, in the regex shown you have backslashed a close paren. That's at least part of what is driving you crazy.

    jimk

      The objective is to replace the following string:

      test("test

      with test("GC_test

      i.e. put GC_ before the second "test". Hope that answers your query.

      As well backslashing the open paren didn't work. That was my initial solution. But I did try your suggestion but no luck.

      Please help; and thanks
        #!/usr/bin/perl -w use strict; my $str_in = 'test("test'; my $str_out = $str_in; #$str_out =~ s/"t/"GC_t/; $str_out =~ s/"/"GC_/; print "string 1: $str_in\n"; print "string 2: $str_out\n";

        produces

        string 1: test("test string 2: test("GC_test

        Am I missing something here?

        Update: Just match the double quote (original match commented out above). That works just well, assuming there is only one double quote in the string.

Re: Replace strings with "("
by trammell (Priest) on Jan 30, 2005 at 00:29 UTC
    Are you sure you have the correct match string? There's no whitespace or unprinted characters in there? What does
    warn("string matches") if $string =~ /test\("test/;
    do?
Re: Replace strings with "("
by gube (Parson) on Jan 30, 2005 at 09:35 UTC

    Hi, Try it simple

    my $str = 'test("test..hai")'; $str =~ s#\Qtest("test\E#test("GC_test#gsi; print "$str";
Re: Replace strings with "("
by ambrus (Abbot) on Jan 30, 2005 at 22:13 UTC

    Maybe you could try s/(test\(\W)(test)/$1GC_$2/ as the quotation mark might not be the same in the regexp and in the string.