Tapatalk

How to move minus signs from right to left on all floating point values in a file?

How to move minus signs from right to left on all floating point values in a file?

14
Basic UserBasic User
14

    Sep 22, 2020#1

    A value in a CSV file is 2.33- (minus is on the right side). How do we put all the minus signs to the left side like -2.33 (minus is on the left side) using a Perl regular expression replace in UltraEdit?

    6,685587
    Grand MasterGrand Master
    6,685587

      Sep 24, 2020#2

      This task can be done with a Perl regular expression replace all using a backreference with the search string (?<![+\-\d])([\d.]+)-(?![+\-\w]) and the replace string -\1 which is executed from top of the file.

      Explanation for the search string:

      (?<!...) ... This is a negative look-behind which does not match (select) anything. It is just an extra condition for a positive match.

      [+\-\d] ... This expression defines a character class with plus sign, minus sign (in real hyphen-minus), and any digit (0-9). The hyphen-minus must be escaped with a backslash inside a character class definition to be interpreted as literal character and not with the meaning from ... to. The hyphen-minus is always interpreted as literal character outside a character class definition.

      The negative look-behind with this character class makes sure for this regular expression search string that just a floating point value is matched with next character being a minus sign if there is neither a plus sign nor a minus sign nor a digit left to matched string.

      (...) ... This is a marking/capturing group. The string matched by the expression inside the round brackets can be referenced in search or replace string with \1 as done here in replace string to keep the matched floating point value during the replace.

      [\d.]+ ... This character class matches any digit or a dot. + is a multiplier applied on the character class to match one or more digits/dots. So this expression inside the marking group matches/selects the floating point value.

      - ... the next character after floating point value must be a minus sign which is also matched/selected.

      (?!...) ... This is a negative look-ahead which does not match (select) anything. It is just one more extra condition for a positive match.

      [+\-\w] ... This expression defines a character class with plus sign, minus sign (hyphen-minus), and any word character according to Unicode specification.

      The negative look-ahead with this character class makes sure for this regular expression search string that just a floating point value is matched with next character being a minus sign if there is neither a plus sign nor a minus sign nor a word character following the minus sign of the matched string.

      I used following text for testing the Perl regular expression replace:

      2.33-
      +2.34-+40.85
      -20.33--12.743
      10.33-
      5-years old
      128-150
      67424-

      The result after the replace all is:

      -2.33
      +2.34-+40.85
      -20.33--12.743
      -10.33
      5-years old
      128-150
      -67424

      The search string is perhaps much more complex than really needed for your purpose, i.e. if the floating point values are in a CSV file containing just floating point values. But there was nothing posted in which context the floating point values with a minus sign (hyphen-minus) are on right side instead of left side of the floating point values.

      PS: There is a real minus sign also defined by the Unicode specification, but in text files is used usually the hyphen-minus. It is advisable to use in documents like in a Microsoft Word document or an HTML file the real minus sign character for a minus sign and not the hyphen-minus character. That makes a big difference on a find/replace in documents for negative values and also on display of the minus sign. The real minus sign is as long as the plus sign while the hyphen-minus is shorter than the plus sign. The real minus sign is vertically positioned in the middle of a digit like the plus sign while the hyphen-minus is vertically positioned at ⅔ of the height of a lower case letter which is below the middle of a digit.
      Best regards from an UC/UE/UES for Windows user from Austria

      14
      Basic UserBasic User
      14

        Sep 25, 2020#3

        Thank you.....
        bro thank so much ..........
        u helped lot