Get 10 lines before specific word with regular Expression
-
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 -
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
.*
beforesucceeded
non-greedy with by using.*?
instead -
@peterjones
I turned of the checkbox and used this
(.*\R){10}^.*?succeeded.*\R
but not working -
@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
-
@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
-
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). -
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:
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.