How to fill space between two words (data columns) with spaces?

How to fill space between two words (data columns) with spaces?

7
NewbieNewbie
7

    Nov 22, 2018#1

    Hello,

    I'm fighting with a regex to fill the space between two words with spaces. 

    Situation:
    I've a text file with approx 500 lines. Every line contains two "words", which are separated with whitespaces (one or more). The length of the first word varies from 3 to 30 character. The length of the second "word" varies from 1 to 10 characters. For better readability and maintenance purposes, the second word should start at the 40th character, regardless of whether the first word is 3 or 30 characters long. The space between the two words should be filled with white spaces.

    Source format
    2018-11-22_15h22_47.png (5.89KiB)

    Target format
    2018-11-22_15h23_00.png (6.24KiB)

    I tried:
    Find what: [^\s]*)([\s]*)([^\s]*) 
    Replace with: (\1 \2){40} \3

    But the search expression doesn't work.

    Could anyone give me a hint to solve this problem with regex?

    Thanks for your support.

    Manuel

    19176
    MasterMaster
    19176

      Nov 22, 2018#2

      Hi Manuel,

      everything is possible. Warning! Following replaces are a little crazy ;)

      1. Normalize spaces
        F: ^\h*(\S{3,30})\h+(\S+)$
        R: \1                                       \2


      2. Adjust spaces according to the length of the 1st word
        F: ^\h*(?|(\S{30})\h{30}|(\S{29})\h{29}|(\S{28})\h{28}|(\S{27})\h{27}|(\S{26})\h{26}|(\S{25})\h{25}|(\S{24})\h{24}|(\S{23})\h{23}|(\S{22})\h{22}|(\S{21})\h{21}|(\S{20})\h{20}|(\S{19})\h{19}|(\S{18})\h{18}|(\S{17})\h{17}|(\S{16})\h{16}|(\S{15})\h{15}|(\S{14})\h{14}|(\S{13})\h{13}|(\S{12})\h{12}|(\S{11})\h{11}|(\S{10})\h{10}|(\S{9})\h{9}|(\S{8})\h{8}|(\S{7})\h{7}|(\S{6})\h{6}|(\S{5})\h{5}|(\S{4})\h{4}|(\S{3})\h{3})(\h+)(\S+)$
        R: \1\2\3

      BR, Fleggy

      6,675585
      Grand MasterGrand Master
      6,675585

        Nov 22, 2018#3

        I suggest following:
        1. Run a Perl regular expression Replace all with search string (\S+)$ and the replace string consists of 40 spaces and \1 to insert 40 spaces left to last word on a line.
        2. Run a Perl regular expression Replace all with search string ^.{40}\K + and the replace string is an empty string to delete all spaces after first 40 characters up to next non-whitespace character.
        The first search string searches with \S+ for a non-whitespace character one or more times with marking the found string which must be found at end of line/file with matching the line ending characters. The marked string is back-referenced with \1 in replace string.

        The second search string searches for string at beginning of a line/file with .{40} for any character except newline characters exactly 40 times which are not matched because of \K and then for one or more spaces which are matched (selected) and so deleted because of empty replace string.

        UltraEdit uses the Boost Perl Regular Expression library.
        Best regards from an UC/UE/UES for Windows user from Austria

        7
        NewbieNewbie
        7

          Nov 22, 2018#4

          Hello Fleggy,

          Thanks for the expression, works like a charm, although if I don't understand everything of the expression :-). I will analyze it.

          Manuel

            Nov 22, 2018#5

            Hello Mofi,

            Your expression works also like a charm. I've also analyze the regex to understand it :-)

            Manuel

            19176
            MasterMaster
            19176

              Nov 23, 2018#6

              Hi Manuel,

              Mofi's approach is the best. Anyway, I have another more complex three step solution if you are interested :D

              Fleggy

              7
              NewbieNewbie
              7

                Nov 23, 2018#7

                Hi Fleggy,

                sure, "Many roads lead to Rome" ;-)

                Manuel

                  Nov 23, 2018#8

                  Thanks Mofi.

                  Some lines of the text file are comment lines, those lines begin with "!" or "#". So I modified the regex expression.

                  Code: Select all

                  Find: (^[^!#]\S+)(\s+)(\S+)$
                  Replace: \1                                       \3
                  

                  Code: Select all

                  FInd: ^(?![!#]).{40}\K +
                  Repace: <nothing>
                  
                  👍