Community
    • Login

    How find range of lines by their number?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 1.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Obada KurdiO
      Obada Kurdi
      last edited by

      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

      Scott SumnerS 1 Reply Last reply Reply Quote 0
      • Scott SumnerS
        Scott Sumner @Obada Kurdi
        last edited by

        @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 ).

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors