Need Guidance -- How can I find a specific line segment and remove all spaces and tabs from ONLY that segment.



  • Ex. I want to find all “EQD” lines and remove all spaces and tabs from JUST those lines.

    See below sample data, as you can see 2 of the 3 EQD lines have a space. I need to remove any “space” or “tab” from just that line.

    EQD+CN+ZMLU XXXXXXX+45GP+++5
    NAD+CA+ZMLU:172:ZZZ
    LOC+147+0260808::5
    FTX+AAA+++ALUMINUM ARTICLES
    MEA+WT++KGM:16549
    LOC+9+DOHAI:139:6
    LOC+11+USSAV:139:6
    RFF+BM:1
    EQD+CN+ZMLU XXXXXXX+45GP+++5
    NAD+CA+ZMLU:172:ZZZ
    LOC+147+0260202::5
    FTX+AAA+++ALCOHOLIC BEVERAGES
    MEA+WT++KGM:27797
    LOC+9+JMKIN:139:6
    LOC+11+USSAV:139:6
    RFF+BM:1
    EQD+CN+ZMLUXXXXXXX+45GP+++5
    NAD+CA+ZMLU:172:ZZZ

    I appreciate everyone’s time that is looking into this.

    Cheers



  • @John-Tee said in Need Guidance -- How can I find a specific line segment and remove all spaces and tabs from ONLY that segment.:

    I want to find all “EQD” lines and remove all spaces and tabs from JUST those lines.

    Using the Replace function we have:
    Find What:(?-is)(?=^EQD).*?\K\h
    Replace With:nothing in this field empty

    As this is a regular expression the search mode MUST be regular expression and you must use the “Replace All” button as I have used the \K meta character in the expression. Run it multiple times if any line has more than 1 instance of a blank space or tab (you can see when it’s completed when the Replace Window states “0 occurances were replaced in entire file”. This expression will remove ONLY one at a time from each of the lines with EQD in then, it will not touch any line with eqd

    Terry



  • Hello, @john-tee, @terry-r and All,

    Ah, yes, @terry-r, we may also use this variation :

    (?-is)^(?=EQD).*?\K\h


    Now, if more than one horizontal blank char is present, in a line beginning with EQD, these two syntaxes do not work !

    In that case, we need to use the generic regex, belox, discussed in other posts, like this one :

    https://community.notepad-plus-plus.org/post/62799

    SEARCH (?-i:BSR|(?!\A)\G)(?s:(?!ESR).)*?\K(?-i:FR)

    REPLACE RR

    where :

    • FR (Find Regex ) is the regex which defines the char, string or expression to be searched

    • RR (Replacement Regex ) is the regex which defines the char, string or expression which must replace the FR expression

    • BSR ( Begin Search-region Regex ) is the regex which defines the beginning of the area where the search for FR, must start

    • ESR ( End Search-region Regex) is the regex which defines, implicitly, the area where the search for FR, must end


    For @John-Tee’s need, we have :

    • FR = \h

    • RR = Empty string

    • BSR = ^EQD

    • ESR is implicit and we don’t need the (?s:(?!ESR).)*? syntax . Indeed, due to the (?-s) modifier, the range of chars, matched with the part .*? cannot go beyond the current line, anyway, as the line-break char(s) are not allowed !

    Thus, if several space or tabulation characters may appear in the EQD...... lines, the appropriate regex S/R is rather :

    SEARCH (?-s)(?-i:^EQD|(?!\A)\G).*?\K\h

    REPLACE Leave EMPTY

    Best Regards,

    guy038



  • @guy038 said in Need Guidance -- How can I find a specific line segment and remove all spaces and tabs from ONLY that segment.:

    @guy038 @Terry-R

    Is there a way to adjust the provided SEARCH: " (?-s)(?-i:^EQD|(?!\A)\G).*?\K\h " to further clean spaces and tabs for all the following lines at once: EQD, NAD, LOC, MAE, RFF - but don’t touch line FTX?

    I welcome your feedback and thank you both again for your time.



  • @John-Tee said in Need Guidance -- How can I find a specific line segment and remove all spaces and tabs from ONLY that segment.:

    Is there a way to adjust the provided SEARCH: " (?-s)(?-i:^EQD|(?!\A)\G).*?\K\h " to further clean spaces and tabs for all the following lines at once: EQD, NAD, LOC, MAE, RFF - but don’t touch line FTX?

    Certainly. The changes are:
    for my regex it becomes (I made an additional change putting the ^ outside of the lookahead)
    (?-is)^(?=EQD|NAD|LOC|MAE|RFF).*?\K\h
    and for @guy038 regex it would be
    (?-s)(?-i:^(EQD|NAD|LOC|MAE|RFF)|(?!\A)\G).*?\K\h

    You can easily see how it expands to any additional codes, so if you need to change it, you should be able to do so easily. Try that.

    Terry



  • @Terry-R Thank you very much the new update works great just what I needed. All the best and thank you @Terry-R and @guy038 for your time and guidance. Cheers


Log in to reply