Replacing the string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramesh54
    New Member
    • Jul 2008
    • 37

    #1

    Replacing the string

    Hello All,

    I would like the replace the following string,

    TRANS -847.0 -672.0 -71.0

    as

    TRANS -840.0 -660.0 -62.0

    I am able to replace the string when the numbers are positive without decimals using \d and \s syntax. But the above expressions have negative sign and decimals. How could i formulate that using regexp.

    Ramesh
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    What have you tried thus far? I would love to see what you come up with and will then help you from there. Do not forget to post your code (in code tags) and any errors or results.

    Regards,

    Jeff

    Comment

    • ramesh54
      New Member
      • Jul 2008
      • 37

      #3
      Code:
      #!\usr\bin\perl
      use warnings;
      use strict;
      open IN,"+< z.txt";  
      open OUT,"> t.txt";  
      while (<IN>) {
      s/(\d+)\s+(\d+)\s+(\d+)/ ($1+7).$2. ($3+12).$2.($4+9)/g;
      print OUT;
      }
      This works when numbers are positive integers. My case is for decimals and negative numbers.
      Last edited by eWish; Aug 6 '08, 01:01 AM. Reason: Added code tags

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Do not forget to post your code (in code tags)
        oops...... hehehe

        Comment

        • eWish
          Recognized Expert Contributor
          • Jul 2007
          • 973

          #5
          Originally posted by KevinADC
          oops...... hehehe
          Maybe numberwhun won't notice.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Code:
            my $str = 'TRANS -847.0 -672.0 -71.0';
            $str =~ s/(\S+)\s+(-?\d+\.?\d?)\s+(-?\d+\.?\d?)\s+(-?\d+\.?\d?)/sprintf "$1 %.1f %.1f %.1f", $2+7,$3+12,$4+9/eg;
            print $str;

            Comment

            • ramesh54
              New Member
              • Jul 2008
              • 37

              #7
              Thanks Kevin for the help. It replaces the negative and decimal numbers.

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by ramesh54
                Thanks Kevin for the help. It replaces the negative and decimal numbers.
                Next time use the CODE tags, OK? If not, Jeff might ban you from the forum.

                Comment

                • numberwhun
                  Recognized Expert Moderator Specialist
                  • May 2007
                  • 3467

                  #9
                  Originally posted by KevinADC
                  Next time use the CODE tags, OK? If not, Jeff might ban you from the forum.
                  I am not that cruel, I give a couple warnings. :-) Don't you have a rebellion to worry about Kevin?

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by numberwhun
                    I am not that cruel, I give a couple warnings. :-) Don't you have a rebellion to worry about Kevin?

                    No, the rebellion was postponed, maybe cancelled, depends on if my wife comes to her senses.

                    Comment

                    Working...