Replace numbers from position 20 to 30 in the middel af a line.

  • I have a file with 10000+ lines - I know the alt+mouse to mark text in the middle of the lines, but with 10000+ lines it is difficult to concentrate to the bottom… :-)

    Can I use regular expressions or maybe a plugin to replace all number from position 20-30 in each line to 0000000000

    The represent a amount which is different in every line.

    Example file:

    My wish :-)

  • Based on your example, I assume you mean positions 21-30: i.e., keep 1-20, replace 21-30, keep anything else

    Find:      ^(.{20}).{10}
    Replace:   (\1)0000000000

    The parentheses in the Replace string are for readability, and could have been written as

    Replace:   \10000000000

    [ UPDATE: Make sure “Regular Expression” is selected. :-) ]

    Explanation: Find

    ^ = start of string
    (.{20}) = match the first twenty characters, and store them in \1 (. matches any character)
    .{10} = match the next 10 characters, and don't store them (They won't be part of the ending string

    Explanation: Replace

    (\1)       = recall the \1 from the match, and put the characters there
    0000000000 = put 10 raw 0s in the text

    The rest of the line, which isn’t part of the match, will stay as-is.

  • Thank you very much, just what I needed… :-) :-)

    I have just changes 20924 lines in no time…

  • Hello , Tino and Peter,

    Congratulations to you, both ! Tino, for nicely exposing your wish and Peter for your full explanation, about the proposed regex :-)

    An other S/R, using the \K syntax, could be :

    Find what : ^.{20}\K.{10}

    Replace with : 0000000000

    Notes :

    • As soon as the first twenty characters of the current line are matched, the \K feature forces the regex engine to forget anything previously matched and reset the regex engine position !

    • So the final match is, only, the ten characters, located between the columns 21 and 30

    • … which is, simply, changed into the 0000000000 string :-)

    Remark :

    • You must use the Replace All button, only. The step by step replacement, with the Replace button, does NOT work, when the search regex contains a \K form !

    Best regards,