• Login
Community
  • Login

How to automatically add space on both sides of the operator

Scheduled Pinned Locked Moved General Discussion
13 Posts 7 Posters 6.5k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G
    guy038
    last edited by guy038 Mar 28, 2018, 10:32 PM Mar 28, 2018, 9:59 PM

    Hello, @成轩 and All

    Without additional information, it’s quite hard to plan anything ! Which language are you using ?

    Anyway, I tried to find out a correct regex S/R, which :

    • Trims any trailing horizontal blank characters, before the End of Line

    • Keeps any leading indentation

    • Keeps the blank characters in any range "..........", too

    • Deletes any range of horizontal blank characters, before a comma

    • Normalizes to a single space character, any range of blank characters, even none, PRECEDING, either :

      • Any consecutive range of symbols ! " # $ % & ' * + - / : ; = ? @ \ ^ | ~

      • Any single symbol [ ] ( ) < > { }

      • Any consecutive range of word characters or dot Letter Digit _ .

    Of course, there are still few weird results ! For instance, the sentence “On the 10/27 of that year” would become “On the 10 / 27 of that year” but that other sentence “The value x=a*10/27+b” would produce the sentence “The value x = a * 10 / 27 + b” which is quite correct !

    For example, the statement “x=(((a+5)/(z-7/b)-sin(30))*Log(6))” would be developed as “x = ( ( ( a + 5 ) / ( z - 7 / b ) - sin ( 30 ) ) * Log ( 6 ) )” with, certainly, too many space characters than necessary ;-))


    A last example : here is, below, a simple Python script, regarding Unix Timestamp, with a very, very bad presentation ! You may select the option View > Show Symbol > Show All Characters to visualize all the blank characters

    Day        =        28			
    Month=3
    Year		=			2018
    Hours			=15                
    Minutes=19
    Seconds		=0
    		                    
    			# This small script get any Unix Timestamp
    			#        with the different time variables above
    			#        		defined with a correct numeric value
    
    Leap=int(Year    %4==0)-int(     Year%100==0)+int(Year%400==0)                            # = 1 if a is a LEAP year, = 0 if NOT
    
    Leap_Years=(Year      -1)//4-(Year-1)     //100+(Year-1)//400-477                           # = TOTAL of LEAP years, from 1970 to the year (Year-1)
    			
    Calendar_Day   =   round(30.57*(Month-1)-2*int(Month   >   2))+Day+int(Leap==1)		*		int(Month>2) # = TOTAL of days, from the 01/01 of Year, to Day, included
    
    Total_Days=(Year-1970)*365+Leap_Years+Calendar_Day-1                             # = TOTAL of days, from 01/01/1970, till (Day-1) of Year
                                      
    Unix_Time=Total_Days*86400+3600*Hours    +       60*Minutes+Seconds                          #  UNIX time, in seconds
    
    
    print("And,    here are.... the     results  :  ",Leap       ,Leap_Years,          Calendar_Day,Total_Days,Unix_Time, "<     Good Bye     >")
    
            # The results are :
    		
    		# Leap=0
            #		Leap_Year		=		12
            # Calendar_Day = 87
            #            Total_Days = 17618
            # Unix_Time		=					1522250340
    

    • Open the Replace dialog ( Ctrl + H )

    • In the Find what: zone, type in the regex :

    (?-s)(\h+\R)|(\h*,)|(^)?\h*(".*?"|[]()<>{}[]|[!"#$%&'*+/:;=?@\\^`|~-]+|[\w.]+)
    
    • In the Replace with: zone, type in the regex
    (?1\r\n:(?2,:(?3$0:\x20\4)))
    
    • Tick the Wrap around option

    • Click, once, on the Replace All button ( or several times on the Replace button

    Et voilà ! You should get the following new text :

    Day = 28
    Month = 3
    Year = 2018
    Hours = 15
    Minutes = 19
    Seconds = 0
    
    			# This small script get any Unix Timestamp
    			# with the different time variables above
    			# defined with a correct numeric value
    
    Leap = int ( Year % 4 == 0 ) - int ( Year % 100 == 0 ) + int ( Year % 400 == 0 ) # = 1 if a is a LEAP year, = 0 if NOT
    
    Leap_Years = ( Year - 1 ) // 4 - ( Year - 1 ) // 100 + ( Year - 1 ) // 400 - 477 # = TOTAL of LEAP years, from 1970 to the year ( Year - 1 )
    
    Calendar_Day = round ( 30.57 * ( Month - 1 ) - 2 * int ( Month > 2 ) ) + Day + int ( Leap == 1 ) * int ( Month > 2 ) # = TOTAL of days, from the 01 / 01 of Year, to Day, included
    
    Total_Days = ( Year - 1970 ) * 365 + Leap_Years + Calendar_Day - 1 # = TOTAL of days, from 01 / 01 / 1970, till ( Day - 1 ) of Year
    
    Unix_Time = Total_Days * 86400 + 3600 * Hours + 60 * Minutes + Seconds # UNIX time, in seconds
    
    
    print ( "And,    here are.... the     results  :  ", Leap, Leap_Years, Calendar_Day, Total_Days, Unix_Time, "<     Good Bye     >" )
    
            # The results are :
    
    		# Leap = 0
            # Leap_Year = 12
            # Calendar_Day = 87
            # Total_Days = 17618
            # Unix_Time = 1522250340
    

    Of course, the trailing comments and, generally, the possible lists are not aligned, too :-( Anyway, I just ran that Python3 script, after the S/R modifications, without any trouble !

    You may use this Unix Timestamp Converter to verify my calculus !

    Cheers,

    guy038

    P.S. :

    • This Python example is quite raw and simple ! My initial program, about conversions Date <–> Unix Timestamp, was written in the old Qbasic language and some months ago, I simply translated the main part, as an Python3 exercise , about mathematics ;-))

    • About 00h30, in France ! So, I’m a bit lazy to explain how this regex S/R works ! May be next time, if you offer me… some beer ;-))

    1 Reply Last reply Reply Quote 0
    • G
      Gogo Neatza
      last edited by Mar 29, 2018, 12:29 AM

      @成轩 : Spend less than ten seconds to write your own Macro !

      S 1 Reply Last reply Mar 29, 2018, 12:02 PM Reply Quote 0
      • S
        Scott Sumner @Gogo Neatza
        last edited by Mar 29, 2018, 12:02 PM

        @Gogo-Neatza said:

        Spend less than ten seconds to write your own Macro !

        I’m not sure how THAT is helpful in any way…please explain if you can…

        @guy038 said:

        …the statement “x=(((a+5)/(z-7/b)-sin(30))*Log(6))” would be … “x = ( ( ( a + 5 ) / ( z - 7 / b ) - sin ( 30 ) ) * Log ( 6 ) )” with…too many space characters

        It is very subjective (i.e., everybody likes it their own way), but I definitely agree that this is too spacey:

        x = ( ( ( a + 5 ) / ( z - 7 / b ) - sin ( 30 ) ) * Log ( 6 ) )
        

        but I like this version, which doesn’t add space adjacent to ( or ) :

        x = (((a + 5) / (z - 7 / b) - sin(30)) * Log(6))
        

        The original is just visually painful …ouch! :

        x=(((a+5)/(z-7/b)-sin(30))*Log(6))
        

        I think I will make the modification to the regex to avoid the “too spacey” condition and then record it as a Replace All, In Selection macro, for handy use when I get a short snippet of code from (e.g.) stackoverflow and don’t like the original whitespacing of an equation. (Is this what @Gogo-Neatza meant?!)

        BTW, @guy038, I like this site for timestamp conversion.

        1 Reply Last reply Reply Quote 0
        • V
          vtech7
          last edited by Mar 30, 2018, 6:33 AM

          Hi, in order to convert the following sample of code:

          x=(((a+5)/(z-7/b)-sin(30))*Log(6))

          to this one: (without double spacing)

          x = ( ( ( a + 5 ) / ( z - 7 / b ) - sin ( 30 ) ) * Log ( 6 ) )

          find:

          (([\w])([^\w|\s]))|(([^\w|\s])([\w]))|(([^\w|\s])([^\w|\s]))

          replace:

          (?1\2 \3)(?4\5 \6)(?7\8 \9)

          1 Reply Last reply Reply Quote 0
          • V
            vtech7
            last edited by Mar 30, 2018, 8:32 AM

            I just forgot to mention that the above find and replace must apply 2 times (for the given sample code).

            S 1 Reply Last reply Mar 30, 2018, 12:26 PM Reply Quote 0
            • S
              Scott Sumner @vtech7
              last edited by Mar 30, 2018, 12:26 PM

              @vtech7

              Hmmm…or just put the spaces in by this process:

              • position the caret appropriately
              • press the space bar once
              • REPEAT above steps until desired effect is achieved
              1 Reply Last reply Reply Quote 0
              • gwarahG
                gwarah
                last edited by gwarah Apr 3, 2018, 10:27 AM Apr 3, 2018, 10:25 AM

                Maybe this issue would be a case to new feature request (or plugin suggestion) in a way to put spaces between chars of current line, and the user lists in a field the sequences that wouldn’t applied (ex. ++, —,…). There would be a general sequence list and others to each language.

                1 Reply Last reply Reply Quote 0
                • 成
                  成轩
                  last edited by Apr 4, 2018, 2:44 PM

                  I also hope that such a plugin can be selected for different languages. I hope it can automatically determine the operator I entered and then automatically add spaces. Although regular expression processing can achieve the above operations, I think it is not too friendly and convenient

                  1 Reply Last reply Reply Quote 0
                  • 成
                    成轩
                    last edited by Apr 4, 2018, 3:21 PM

                    Thank you all for answering questions

                    1 Reply Last reply Reply Quote 0
                    • Fahim AnwerF
                      Fahim Anwer
                      last edited by Apr 17, 2018, 4:09 AM

                      You could try to use “find and replace” feature, set to consider regular expressions, and replace all no-space (x) char with x followed a space in a current selection.

                      Save it as a macro and apply in other parts of code.
                      https://10bestgame.com/instagram-cool-captions/

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors