Community
    • Login

    Add new lines after matching lines (was:'Hello ALL')

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    11 Posts 4 Posters 785 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.
    • EkopalypseE
      Ekopalypse
      last edited by guy038

      @Rajeev-Gupta said in Hello ALL,:

      I’m not 100% sure I understand your request but from the example
      you’ve given this might do it

      find what:(set @queryName = '.+'\h*\R)
      repalce with:$1IF X = Y Then

      1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones
        last edited by

        @Rajeev-Gupta :

        @Ekopalypse has provided a first level suggestion, and we would need clarification to get a better answer than that. Though if I had been making my guess, I think you want something like:

        • find = (?-s)set @queryName = '(.*)'\h*(?:(\R)|\z)\K
        • replace = (?2:\r\n)IF X=Y then\r\n something '$1' different\r\n

        compared to Eko’s reply, I understood you to want to grab the XXXX contents and reuse it. I also made sure that if the final line in the file matched, but didn’t have a newline, it will still do the replacement. So if your file were exactly two lines long, with no newline at the end of the second line:

        set @queryName = 'YYYY'
        set @queryName = 'XXXX'
        

        it would become

        set @queryName = 'YYYY'
        IF X=Y then
            something 'YYYY' different
        set @queryName = 'XXXX'
        IF X=Y then
            something 'XXXX' different
        

        As an aside: unfortunately, your topic title with the typ of “Hell” instead of “Hello” has significantly changed the tone of your topic title with expected content including fiery condemnation, which I doubt is what you were going for. :-) Unfortunately, original posters cannot edit the name of their Topic, so we need an admin to step in. However, if you have a suggestion for a more-meaningful title then “Hello All”, it would be helpful. Our resident forum admin @guy038 should be able to re-title it to “Hello All”, or whatever you suggest. (Actually, my default suggestion would be “Add new lines after matching lines, including some data from the match”. But really, it should be up to @Rajeev-Gupta)

        Alan KilbornA 1 Reply Last reply Reply Quote 2
        • Alan KilbornA
          Alan Kilborn @PeterJones
          last edited by

          @PeterJones

          What is the significance of the \K at the very end of your find regex?

          Rajeev GuptaR 1 Reply Last reply Reply Quote 3
          • PeterJonesP
            PeterJones
            last edited by PeterJones

            It resets the match, so that I didn’t have to include an $1 at the beginning to replace the originally-matched line with itself.

            (match) -> $1NEW is equivalent to (match)\K -> NEW – both will just append NEW after the original match.

            Thanks for prompting me to explain that better.

            Also, since the OP hasn’t seen my boilerplate yet, some advice for @Rajeev-Gupta
            -----

            Please Read And Understand This

            The Notepad++ Community is not a search-and-replace (regex) help forum. However, we are often willing to help with regex questions as they pertain to Notepad++, assuming you follow a few simple rules.

            1. Be polite: ask nicely. We’re all here voluntarily.

            2. Be patient: understand that everyone in the Community has their own schedule for when they are and aren’t available to respond: a day or two is not an unreasonable response time.

            3. Show a willingness to learn: we understand that you might be new to regexes.

              • However, the more times we have given you example regexes, the more we will expect you to show some effort.

              • Try to craft your own regex. If it doesn’t work, show us what you tried; explain why you tried that (it shouldn’t be a random guess); explain how the non-working results are different from your expectations.

            4. Please read the manual. We’re likely to be lenient at first, giving a freebie or two. However, the more times you ask regex questions, the more we will expect that you have read and understood the manuals:

              • the official Notepad++ searching using regular-expressions docs

              • this forum’s regex FAQ

            5. Help us help you:

              • Format your data using the the </> button (“code”, aka “literal text”), but leave your normal discussion as normal text (don’t put your whole post in literal-text mode)

              • Be clear about what you want to match; be clear about what you don’t want to match; give examples of each. What doesn’t match is sometimes more important than what does match in order to formulate a reasonable regex for your needs.
            6. We’re not here to do your work. We are here to help you use Notepad++ to its fullest. We expect you to learn from our answers.

            Alan KilbornA 1 Reply Last reply Reply Quote 4
            • Alan KilbornA
              Alan Kilborn @PeterJones
              last edited by guy038

              @PeterJones said in Hello ALL,:

              It resets the match, so that I didn’t have to include an $1 at the beginning to replace the originally-matched line with itself.
              (match) -> $1NEW is equivalent to (match)\K -> NEW – both will just append NEW after the original match.

              Ah, nice one. I don’t think I’ve seen that usage of \K before.

              1 Reply Last reply Reply Quote 1
              • PeterJonesP
                PeterJones
                last edited by

                I picked up the \K-for-add-after-match idiom from @guy038 and/or @Terry-R somewhere along the way, and it’s started to stick to my regexes.

                Alan KilbornA 1 Reply Last reply Reply Quote 3
                • Alan KilbornA
                  Alan Kilborn @PeterJones
                  last edited by

                  @PeterJones

                  It turns the entire match into a zero-length assertion. But, it is worth pointing out that it only works if you are doing a “Replace All” and not an interactive “FindNext-then-Replace”.

                  1 Reply Last reply Reply Quote 1
                  • Rajeev GuptaR
                    Rajeev Gupta @Alan Kilborn
                    last edited by

                    @Alan-Kilborn Thanks for reply – Its typo mistake - Extremply sorry for that

                    I will explain in detail - I have multiple files around 400 where I have to Add new line code for example

                    In one file it is - set @queryName = ‘InsertCNameforLessThan15’;
                    and other file it set @queryName = ‘InsertCNameforLessThan20’;
                    any other file set @queryName = ‘Table Update’;

                    So set @queryName = ‘Table Update’;
                    this value is dynamic
                    I dont want to change anything in this string I have to add some code after this line

                    like - 1 – set @queryName = ‘InsertCNameforLessThan15’;
                    if @DebugMode > 1
                    begin
                    ********************
                    END

                    similer for others
                    set @queryName = ‘InsertCNameforLessThan20’;
                    if @DebugMode > 1
                    begin
                    *********
                    END

                    Hope now its very clear to you - -

                    Thanks
                    And again sorry for wrongly word.

                    1 Reply Last reply Reply Quote 0
                    • PeterJonesP
                      PeterJones
                      last edited by PeterJones

                      @Rajeev-Gupta ,

                      Hope now its very clear to you

                      You apparently didn’t read in my boilerplate where I explained how to mark the data as code using the </> button that’s on the toolbar when you are writing your post. It makes it much easier to know for sure what your data is without the forum mangling characters, and to distinguish your narrative from your data. Please use that.

                      Further, though you used more words to explain, what you didn’t do is say whether one or both of our solutions works for you.

                      Also, that semicolon is new information, which changes the regex. If you want helpful answers, you need to give us representative examples of your data, or you are going to have to be willing to edit the regex to meet your needs. And if you ask more questions, you will have to tell us what didn’t work for you (ie, did it not match anything? or did it match something, but not give the exact replacement you expected? or …)

                      Please help us help you by formatting the text as has been requested, and giving us example before and after data, without changing the expectations every post.

                      If I modify @Ekopalypse’s expression to include the semicolon and to use your more-specific replacement example:

                      • find = (?-s)(set @queryName = '.+';\h*\R) – I added (?-s) to enforce .-does-not-match-newline, and added the semicolon that you didn’t tell us about.
                      • replace = $1if @DebugMode > 1\r\nBEGIN\r\n********\r\nEND\r\n – I changed the IF statement, and included BEGIN/END wrappers

                      It turned the document:

                      set @queryName = 'InsertCNameforLessThan15';
                      blah
                      blah
                      
                      set @queryName = 'InsertCNameforLessThan20';
                      halb
                      halb
                      

                      into

                      set @queryName = 'InsertCNameforLessThan15';
                      if @DebugMode > 1
                      BEGIN
                      ********
                      END
                      blah
                      blah
                      
                      set @queryName = 'InsertCNameforLessThan20';
                      if @DebugMode > 1
                      BEGIN
                      ********
                      END
                      halb
                      halb
                      

                      Please notice how using the </> button has put the text into a plaintext box, and the ASCII quotes '...' do not get turned into smartquotes ‘...’, so it’s a more-accurate representation of your data.

                      Using Eko’s as the base, I was able to generate something that I think meets your expectations, assuming that **** isn’t supposed to include anything from the original match.

                      Using mine as the base,

                      • find = (?-s)set @queryName = '(.*)';\h*(?:(\R)|\z)\K
                      • replace = (?2:\r\n)if @DebugMode > 1\r\nBEGIN\r\n***** this allows your replacement to embed the matching string from the match = '$1' *****\r\nEND\r\n

                      (if we haven’t made it clear in our answers, all of these answers assume ☑ regular expression is enabled)

                      For my regex, modified for semicolon ; and your more-specific replacement, the *** will be able to include the text from inside your queryName string, so

                      set @queryName = 'InsertCNameforLessThan15';
                      blah
                      blah
                      
                      set @queryName = 'InsertCNameforLessThan20';
                      

                      which has the last line end with no newline character, will become

                      set @queryName = 'InsertCNameforLessThan15';
                      if @DebugMode > 1
                      BEGIN
                      ***** this allows your replacement to embed the matching string from the match =  'InsertCNameforLessThan15' *****
                      END
                      blah
                      blah
                      
                      set @queryName = 'InsertCNameforLessThan20';
                      if @DebugMode > 1
                      BEGIN
                      ***** this allows your replacement to embed the matching string from the match =  'InsertCNameforLessThan20' *****
                      END
                      

                      Both of these meet your expressed needs, and you should have been able to figure out how to put the semicolon after the closing ' to make it match for you.

                      At this point, I am done with freebies for you. If you show more effort – properly formatting your post so the data comes through, giving us representative data, telling us whether or not ours work, and what isn’t right (for example, saying “when I tried it with data xyz, your expression made it pdq, but I wanted pdqu; I tried modifying the regex to rr;r, because I thought adding the semicolon would make it match more accurately, but that made it say pdqv, which still isn’t quite what I wanted”) – if you show that level of effort, so that we can tell you’re trying to learn from us rather than mooch off us, then I’ll be willing to answer more well-formed questions about this regex. If you don’t, well, we’ve given enough that assuming your data was correct, it will work for you.

                      1 Reply Last reply Reply Quote 1
                      • Rajeev GuptaR
                        Rajeev Gupta
                        last edited by

                        @PeterJones said in Add new lines after matching lines (was:'Hello ALL'):

                        (?-s)set @queryName = '(.)';\h(?:(\R)|\z)\K

                        Hi PeterJones – Thanks a lot for your valuable time, your solution working successfully, I am new here but it’s quite interesting to learn regex.

                        :) Thanks you Sir

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