search . and replace with /../

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Raju Sathliya
    New Member
    • Jul 2008
    • 7

    search . and replace with /../

    I have writen a small script to replace the . with /../
    which is as follows.

    $var = "DataAccess\Dat aAccess.";
    $var =~ s/(.)/(\)..(\)/;
    print "value is : $var";

    But in this case when I run this script it gives me result as :
    value is : ()..()ataAccess DataAccess.

    which not as per my expectation.

    I want the result some thing like this : value is : value is : DataAccess\Data Access\..\

    So what changes should i make in my current script to get desire result.
    Also why it is removing '\' character when run the script.

    -Thanks,
    Raju
    Last edited by Raju Sathliya; Jul 8 '08, 07:33 AM. Reason: modify
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    . is a special character and when u want to replace that you shuld use escape character for that like
    [code=perl]
    $val =~ s/\./\.\./g;

    [/code]

    Raghu

    Comment

    • Raju Sathliya
      New Member
      • Jul 2008
      • 7

      #3
      Originally posted by gpraghuram
      . is a special character and when u want to replace that you shuld use escape character for that like
      [code=perl]
      $val =~ s/\./\.\./g;

      [/code]

      Raghu
      Thanks Raghu for your usefull solution...
      But One problem is still there..when i run the following script

      $var = "DataAccess\Dat aAccess.";
      $var =~ s/\./\\.\./;

      then output is : DataAccessDataA ccess\..

      but in this output why '\' is removed ???....I want this in
      output : DataAccess\Data Access\.. formate

      Can you please suggest me some solution to maintain this \ in my output string.

      Thanks,
      Raju

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        you changed the regexp that Raghu posted for you, it should be:

        Code:
        s/\./\.\./g;
        should not be:

        Code:
        s/\./\\.\./g;
        Next time look at the code more carefully.

        It can really be written like this:

        Code:
        $foo = 'this\that.';
        $foo =~ s/\./../g;
        print $foo;
        the '.' has no special meaning on the replacement side of the regxp, only on the search side.

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          the '.' has no special meaning on the replacement side of the regxpThis is a good learning for me.....

          Raghuram

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by gpraghuram
            the '.' has no special meaning on the replacement side of the regxpThis is a good learning for me.....

            Raghuram

            The replacement side of s/// is treated like a double-quoted string, so variable expansion (for example $foo and @foo) and meta character expansion (for example \t and \n) do occur, but a dot in a double-quoted string is just a dot. So anything that can be interpolated/expanded in a double-quoted string will also be expanded/interplolated on the replacement side of s///. Everything else is treated literally. There is a way to alter that behavior however, namely using the "e" modifier on the end of the regexp.

            Comment

            • Raju Sathliya
              New Member
              • Jul 2008
              • 7

              #7
              Thanks to all !!!!
              Finaly solution is working for me....Great Forum !!!!!

              Comment

              Working...