• Login
Community
  • Login

For regex users: how can I add some lines after some other lines (intersect them)

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
19 Posts 4 Posters 3.4k 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.
  • N
    Neculai I. Fantanaru
    last edited by Dec 1, 2018, 11:52 AM

    @guy038 said:

    SEARCH (?-s)^.+\R(?=(?:.+\R){6}(.+(\R)))|(?s)---.+
    REPLACE ?1\1$0\2

    Your regex is GREAT. Buy there is a little problem, @guy038. See this example:

    My mother is home.
    My father is with my sister.
    I have to go home.
    I need some help.
    God is everywhere.
    My dog is with her cat.
    -----------
    https://mywebsite.com/my-link-one.html
    https://mywebsite.com/my-link-two.html
    https://mywebsite.com/my-link-three.html
    https://mywebsite.com/my-link-four.html
    https://mywebsite.com/my-link-five.html
    https://mywebsite.com/my-link-six.html
    

    After search and replace, (test again regex) I cannot find the last line https://mywebsite.com/my-link-five.html

    S 1 Reply Last reply Dec 1, 2018, 2:40 PM Reply Quote 0
    • S
      Scott Sumner @Neculai I. Fantanaru
      last edited by Scott Sumner Dec 1, 2018, 2:40 PM Dec 1, 2018, 2:40 PM

      @Neculai-I.-Fantanaru

      I cannot find the last line https://mywebsite.com/my-link-five.html

      DId you really mean to say?: “I cannot find the last line https://mywebsite.com/my-link-six.html ”

      If so, I can understand why you cannot find it: In your before-text that line (at end-of-file) probably doesn’t have a line-ending on it. Move to the end of that line and press Enter. Then try @guy038’s replacement operation again.

      1 Reply Last reply Reply Quote 1
      • N
        Neculai I. Fantanaru
        last edited by Dec 1, 2018, 2:53 PM

        @guy38 said:

        SEARCH (?-s)^.+\R(?=(?:.+\R){6}(.+(\R)))|(?s)---.+
        REPLACE ?1\1$0\2

        ok, I try again, but another scenario. Suppose there are much more lines. Try this, and you will see the problem.

        My mother is home.
        My father is with my sister.
        I have to go home.
        I need some help.
        God is everywhere.
        My dog is with her cat.
        My mother is home.
        My father is with my sister.
        I have to go home.
        I need some help.
        God is everywhere.
        My dog is with her cat.
        My mother is home.
        My father is with my sister.
        I have to go home.
        I need some help.
        God is everywhere.
        My dog is with her cat.
        -----------
        https://mywebsite.com/my-link-one.html
        https://mywebsite.com/my-link-two.html
        https://mywebsite.com/my-link-three.html
        https://mywebsite.com/my-link-four.html
        https://mywebsite.com/my-link-five.html
        https://mywebsite.com/my-link-six.html
        https://mywebsite.com/my-link-one.html
        https://mywebsite.com/my-link-two.html
        https://mywebsite.com/my-link-three.html
        https://mywebsite.com/my-link-four.html
        https://mywebsite.com/my-link-five.html
        https://mywebsite.com/my-link-six.html
        https://mywebsite.com/my-link-one.html
        https://mywebsite.com/my-link-two.html
        https://mywebsite.com/my-link-three.html
        https://mywebsite.com/my-link-four.html
        https://mywebsite.com/my-link-five.html
        https://mywebsite.com/my-link-six.html
        
        S 1 Reply Last reply Dec 1, 2018, 4:19 PM Reply Quote 0
        • S
          Scott Sumner @Neculai I. Fantanaru
          last edited by Dec 1, 2018, 4:19 PM

          @Neculai-I.-Fantanaru

          So the expectation here, when people provide help is that you read, understand, and learn from what is provided. Sure, if this is a one-time need, I suppose you can just use it blindly and move on, without understanding or trying to learn its application. But this isn’t the case for you, as you are now trying to use the solution provided for a different set of data, without adjusting appropriately the provided solution.

          Big hint: Go back and read the part where @guy038 says:

          As your text contains two blocks of six lines => n = 6 So, the **correct **regex, in your case…

          So see if you can adjust the provided solution to be correct for your new problem.

          1 Reply Last reply Reply Quote 1
          • N
            Neculai I. Fantanaru
            last edited by Dec 1, 2018, 4:22 PM

            yes, indeed. But I thought to update my case with a more complex scenario. In the event that some will want something else.

            S 1 Reply Last reply Dec 1, 2018, 4:28 PM Reply Quote 0
            • S
              Scott Sumner @Neculai I. Fantanaru
              last edited by Dec 1, 2018, 4:28 PM

              @Neculai-I.-Fantanaru

              So you updated the scenario–fine…can you also present a corrected expression to match that scenario…as a demonstration of what you’ve learned from the help you’ve received?

              1 Reply Last reply Reply Quote 0
              • N
                Neculai I. Fantanaru
                last edited by Dec 1, 2018, 4:48 PM

                Yes, it’s about that {6}. In the new scenario I have 18/18 lines. So, it’s working if I change with {18}, such as:

                SEARCH: (?-s)^.+\R(?=(?:.+\R){18}(.+(\R)))|(?s)---.+
                REPLACE ?1\1$0\2

                But what If I have 1000/1110 lines (or if I don’t now the exact number of lines) ?

                I may try something {1,6} but is not good.

                S 1 Reply Last reply Dec 1, 2018, 5:25 PM Reply Quote 0
                • S
                  Scott Sumner @Neculai I. Fantanaru
                  last edited by Dec 1, 2018, 5:25 PM

                  @Neculai-I.-Fantanaru

                  But what If I have 1000/1110 lines (or if I don’t now the exact number of lines) ?

                  It’s not that hard to move the caret to the line above the ----------- and note the line number there by either looking at the line number in the margin (hopefully you have that turned on) or on the status bar. Then move the caret and make sure that the number of lines below the ----------- matches. This seems rather basic so I feel dumb even typing it.

                  But in case this is valuable, here’s an example, using 2 sets of 10 lines each:

                  Line 1: start of first group of data
                  Line 10: end of first group of data
                  Line 11: line of -----------
                  Line 12: start of second group of data
                  Line 21: end of second group of data

                  For the general case of “N” lines:

                  Line 1: start of first group of data
                  Line N: end of first group of data
                  Line N+1: line of -----------
                  Line N+2: start of second group of data
                  Line 2N+1: end of second group of data

                  1 Reply Last reply Reply Quote 0
                  • N
                    Neculai I. Fantanaru
                    last edited by Dec 1, 2018, 7:52 PM

                    the big problem, Scott, is that I have 500 files with more then 1000 lines, which I have to change. And I cannot move the caret to every line above the -----------, etc. I must change all at once with “SEARCH AND REPLACE ALL” function. And I don’t know exactly how many lines are in each files. So, It’s not just one file.

                    S 1 Reply Last reply Dec 1, 2018, 9:24 PM Reply Quote 0
                    • S
                      Scott Sumner @Neculai I. Fantanaru
                      last edited by Dec 1, 2018, 9:24 PM

                      @Neculai-I.-Fantanaru

                      So the premise from the beginning it seems has been that the need is for some few number of pairs of files; mentioning that you had something like 500 would probably have been a good thing to do early on. :-)

                      Do you have a plan for how to combine the pairs of files into one file with the line of --------- separating them? That seems key to the solution described thus far. There are some not-so-difficult ways, but specifying WHICH files and making sure there are the same number of lines in each…I don’t know, but I hope you do, as this is your problem and your data. Perhaps the regular expression can be altered to avoid the dependency on “n”, but unless you have solutions for some of the other technical difficulties, well, then…

                      It seems we are getting very close to advising you that you need a programming language to do this more easily.

                      And yes, people, I do realize this discussion is getting a bit outside the realm of Notepad++ discussion. :-)

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