How find range of lines by their number?



  • I have range of lines with from (1-189). I want to enter an expression in (Replace) to find these lines so I can replace them with something.

    Thank you



  • @Obada-Kurdi

    Okay, so technically it isn’t possible to directly say “Replace lines X through Y” with something else. That being said, there are some ways to achieve the goal.

    In your specific case, with X=1 and Y=189, it becomes fairly easy:

    Step 1: Move your caret to the very top of your file (important)

    Step 2: Execute the following “Replace” operation (not “Replace All”!):

    • Find-what: (?-s)(.*\R){189}
    • Replace-with: whatever you want
    • Search-mode: Regular expression

    However, in the general case (where the starting line X is NOT equal to 1), things are more interesting/tricky. Here’s a solution for that general case, unfortunately it requires you to do some mental math:

    Step 1: Move your caret to the very top of your file (important)

    Step 2: Execute the following “Replace” operation (not “Replace All”!):

    • Find-what: (?-s)(.*\R){S}\K(.*\R){L}
    • Replace-with: whatever you want
    • Search-mode: Regular expression

    where:

    • S is the one less than the desired starting line number, note S = X - 1
    • L is the number of lines it is desired to replace, note L = Y - X + 1

    because (from the problem statement):

    • X is the FIRST line to replace with other data
    • Y is the LAST line to replace with other data

    This technically isn’t a Notepad++ question, but more of a Boost regular expression engine question, so I’m reluctant to put forward this solution here, due to less tolerance lately for non-Notepad++ discussion. However, this does have an important N++ implication regarding where the caret is when the replacement is conducted.

    If this (or ANY posting on the Notepad++ Community site) is useful, don’t reply with a “thanks”, simply up-vote ( click the ^ in the ^ 0 v area on the right ).


Log in to reply