Community
    • Login

    Get 10 lines before specific word with regular Expression

    Scheduled Pinned Locked Moved General Discussion
    7 Posts 3 Posters 2.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.
    • M
      Mohammed Osama
      last edited by PeterJones

      I used this regx (.*\R){10}^.*succeeded.*\R
      to get the previous 10 lins before Succeeded word but the output not correct it retrieve only 4 cases even i have 7 cases matching,
      but its working as excepcted if i change 10 lines to 6 lines
      (.*\R){6}^.*succeeded.*\R
      so is there any restriction for this.

      –
      moderator added code tags

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

        @mohammed-osama ,

        Depending on your setting of .-matches-newline, your multiple .* will match more text than you think. Turn off the checkbox, or prefix your regex with (?-s) , and my guess is that it will work as you expect.

        Oh, also, you can make the one .* before succeeded non-greedy with by using .*? instead

        M 1 Reply Last reply Reply Quote 0
        • M
          Mohammed Osama @PeterJones
          last edited by PeterJones

          @peterjones
          I turned of the checkbox and used this
          (.*\R){10}^.*?succeeded.*\R
          but not working

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

            @mohammed-osama said in Get 10 lines before specific word with regular Expression:

            but not working

            That’s not sufficient for us to be able to help you.

            ----

            Useful References

            • Please Read Before Posting
            • Template for Search/Replace Questions
            • FAQ: Where to find regular expressions (regex) documentation
            • Notepad++ Online User Manual: Searching/Regex
            M 1 Reply Last reply Reply Quote 0
            • M
              Mohammed Osama @PeterJones
              last edited by

              @peterjones said in Get 10 lines before specific word with regular Expression:

              That’s not sufficient for us to be able to help you

              Ok Thank you

              Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn @Mohammed Osama
                last edited by

                @mohammed-osama

                So you just give up?
                Peter was hinting you need to supply more information…to get more help, which people do want to provide (but can’t because “doesn’t work” on its own isn’t really solvable).

                1 Reply Last reply Reply Quote 1
                • PeterJonesP
                  PeterJones @Mohammed Osama
                  last edited by PeterJones

                  @mohammed-osama ,

                  After you gave up, I made a file containing sixteen copies of:

                  twelve
                  eleven
                  ten
                  nine
                  eight
                  seven
                  six
                  five
                  four
                  three
                  two
                  one
                  this line succeeded in being matched
                  
                  

                  When I used the regex you quoted, it showed 16 matches, and each match was the line with “succeeded” plus the 10 lines before it:

                  bf34c849-f868-49da-b09a-47e61dc9be7b-image.png

                  As far as I can tell, that “worked”, based on the problem statement you made in your posts.

                  When you say “but not working”, when it works for us, you have to provide more than “not working” in order for us to understand how your requirements differ from those that you’ve stated.

                  The “Useful References” I linked were to help you ask your question better. and would be useful to help you ask in a way that we can better understand what you want. The “Please Read Before Posting” gives important information about how to format a post, and links to how to format your text in such a way that we can read it (rather than requiring a moderator go in and fix your posts so that the regex are readable; as a moderator, I was willing to do that twice, because I thought it made your questions more accessible, which would make it more likely that you would get your help). The “Template…” is just that, a template that you can use to easily format your search/replace questions so that you give us both example data and what you have already tried. The last two were additional reading that will help you in the future.

                  I went to a lot of effort to make sure you got good help, and yet the extent of your reply was “but not working”. If you try harder, we can continue to try helping. If you give up, then you will never learn how to do what you want. It’s really up to you.

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