replacing spaces with regexp

2. Help
  • Kossak


    I'd like to replace some spaces in one go (currently i have to replace twice to achieve this goal). Below you have example, only the first column needs to be altered (it's csv file)

    The input text:

    id,col a, col b, col c
    1 000,a,b,1 2
    245 000,g,r,1 2
    1 456 754,a,e,1 2
    4 060,a,b,1 2
    125 000,g,r,1 2
    45 476 354,a,e,1 2

    Expected output text:

    id,col a, col b, col c
    1000,a,b,1 2
    245000,g,r,1 2
    1456754,a,e,1 2
    4060,a,b,1 2
    125000,g,r,1 2
    45476354,a,e,1 2

    My current solution is to replace first:
    find: ^(\d+)\s+(\d+)\s+(\d+),
    replace: $1$2$3,

    find: ^(\d+)\s+(\d+),
    replace: $1$2,

    The Question: Can i do both replaces in one go? If i have bigger numbers (like 1 234 567 890) i would have to replace 3 times, etc...

    Thank you for any suggestions.


    Hello Kossak,

    Well, I think that I found a way to achieve your needs :-)

    The processus can be slipt in two stages :

    • Identify the first comma of every line with a specific character, which doesn't exist in your file.

    • Replace every space found, provided that this specific character exists further, in the current line.

    Thus, the spaces will be deleted, as long as they belong to the first column only !

    So, let suppose that the @ character doesn't exist at all, in your file ( Don't choose, as far as possible, a meta-character, like +, ?, which have special sense, in a regex )

    Then, to change the first comma, of every line, by the specific @ character, use the S/R below :

    SEARCH : ^.+?\K,

    REPLACE : @


    You MUST click on the replace All button, ONLY ! The step by step replacement, with the Replace button, doesn't work at all, because of the form \K !

    Notes :

    • The form ^.+?, looks for all the characters from the very beginning of each line till the first comma of each line

    • Just, notice that the form ^.+, would have searched all the characters from the beginning to the last comma of each line !

    • Finally, the \K syntax "forget" everything, placed before \K So the final regex is the comma only, which is replaced by the specific @ character.

    Now, to change any space, only if it's followed, further, on current line, with the specific @ character, use the S/R below :

    SEARCH : (?=.+@) with a space, BEFORE the opening round bracket


    This time, you can, either, click on the Replace or Replace All button

    Notes :
    • The space, at beginning of the search regex is the searched string

    • The (?=.+@) form, is called a lookahead. It's looking, from after the space, for any string of characters which ends with the specific @ character.

    • The string that matched a lookahead, as well as in a lookbehind, is NEVER part of the regex searched. So, the final searched string is a single space, only !

    • If a space is found BEFORE the @ character, the condition (?=.+@) is true. Then, the searched string ( the space ) is deleted, as the replacement part is empty.

    • If a space is found, AFTER the @ character, the condition (?=.+@) is false. Then, no replacement occurs, as expected !

    Et voilà !

    I found, also, an other method, which use one stage only, but, it would needs the recursive replacement feature, that exists in the TextFX plugin !

    You can find good documentation, about the new Perl Common Regular Expressions engine ( PCRE ), used by N++, since the 6.0 version, at the TWO addresses below :

    The FIRST link concerns the syntax of regular expressions in SEARCH

    The SECOND link concerns the syntax of regular expressions in REPLACEMENT

    Best Regards,


    Last edit: THEVENOT Guy 2013-11-14
  • Kossak

    Thank you very much, works as it should. I just switched to Notepad++ few days ago and i learned something today thanks to you :)

    Last edit: Kossak 2013-11-15