Search for a text and copy the previous lines of codes

  • Hi all,

    I’m trying to figure out how can I copy the lines that is before what I am searching. This is similar to

    For example:

    Line1Texttosearch SOMERANDOMTEXT
    Line3copyThisLine IWANTTOCOPYTHISLINE0001
    Line5Texttosearch SEARCHME
    Line7copyThisLine IWANTTOCOPYTHISLINE0002
    Line9Texttosearch SEARCHME

    I want to always mark and copy the 2 lines above the lines where it has the texts “SEARCHME”

    I used (?-s)SEARCHME(?:.\R){3}\K. but it’s for getting the next lines. I need the previous lines. All I know is {3} is the number of lines forward I am getting but don’t know how can I get lines behind.

    The end result when I marked and copied should be:
    Line3copyThisLine IWANTTOCOPYTHISLINE0001
    Line7copyThisLine IWANTTOCOPYTHISLINE0002

  • @Kevin-Alejandrino said in Search for a text and copy the previous lines of codes:

    I used (?-s)SEARCHME(?:.\R){3}\K. but it’s for getting the next lines.

    Your regular expression is confusing as you select the SEARCHME text, then use a non-capture group and then the \K which forgets everything that was captured, which was only the SEARCHME. So all it does is position the cursor 3 lines after the SEARCHME text ONLY IF the following 3 lines are each 1 character followed by a newline sequence. It’s possible what you posted was mangled by the posting engine, so read further and reference the FAQ post I mention to see how to post information correctly.

    Since you haven’t provided information on what you need to actually do with the selected text (you mention mark and copy) I can’t provide a complete solution but what I can provide is a regex that will select 2 lines preceding the SEARCHME text.
    Find What:(?-is)^((?:.+)?\R){2}(?=(.+)?SEARCHME)

    This will find the 2 lines immediately above the line with SEARCHME text in it, regardless where along that line it is, even at the start. The 2 lines selected can even be empty lines, so only a carriage return followed by line feed. Note that as you had the SEARCHME in capitals the regular expression I provided will search on that specifically, so SEARCHme will NOT be valid.

    If you want further help I suggest you read the FAQ section, specifically the post titled FAQ Desk: Formatting Forum Posts which will show you how to include regular expressions and examples so the posting engine does not mangle them.


  • @Terry-R said in Search for a text and copy the previous lines of codes:

    Find What:(?-is)^((?:.+)?\R){2}(?=(.+)?SEARCHME)

    It can also be simplified. I had been working it and went down a slightly different path, both the previous regular expression and this one work the same way.

    I did look at the post you linked to after I’d posted and realised that the posting engine did alter what you typed. You see that the * was removed from 2 areas of the regular expression you provided, hence why I was confused. You do need to insert code like this a special way, which is what the FAQ post shows you, to prevent this happening again.

    The new solution is
    Find What:(?-is)^(.*\R){2}(?=.*SEARCHME)


  • Thanks Terry. It got what I need. Thanks for your help!

  • @Kevin-Alejandrino if you want to skip some string or link just before what you are searching for, say, lookbehind, add (?<!lookbehind)(?<!/a), where lookbehind is a string and /a> is a link - more information is available at

  • @Scott-Nielson said in Search for a text and copy the previous lines of codes:


    This only works if “lookbehind” is an expression of fixed length.
    Perhaps more commonly a user’s desired look-behind match is variable-length; in that case this construct may be used:


  • @Alan-Kilborn so, that should be (?<!lookbehind)\K(?<!/a>)\K right (to skip a look behind and a link just before what is searched for)? Should he (the OP) or anyone else who happens to get to this webpage by searching online put the (?<!lookbehind)\K(?<!/a>)\K at the end or the beginning of the RegEx given by Terry above?

  • @Scott-Nielson

    Consider this text: abcdef

    If one searches for (?<=abc)def then def will be matched, because the lookbehind is “fixed length”. (Note that I discussed a “positive” instead of a “negative” lookbehind, but the behavior is the same).

    As soon as one needs some sort of variable-length lookbehind, there’s trouble, because this construct won’t work: (?<=a.*?)def
    It generates this error:

    A substitute of this works acceptably: a.*?\Kdef
    The use of \K tells the regex engine to “forget the left-occurring match” at this point".
    So, the part to the left MUST match to have an overall match, but the part to the left won’t be part of the final match (which to me sounds like a look-behind assertion).

  • @Scott-Nielson said:

    so, that should be (?<!lookbehind)\K(?<!/a>)\K right (to skip a look behind and a link just before what is searched for)? Should he (the OP) or anyone else…

    Note that I wasn’t considering the OP’s problem when I made my original statement, but was just addressing a caveat involving your earlier reply.

    The OP’s problem wasn’t the clearest in the world. When that happens, I sort of lose interest in the question.

  • @Alan-Kilborn You Sir are a legend. I understood what you mean and I believe it will be of use for others who search online and get here! Thanks a lot.

  • @Alan-Kilborn What should we do if we’re trying to skip 2 or more strings which are looking behind?

  • @Alan-Kilborn to make it more clear, what RegEx can help skip searching for abc in abcdef, xyz in xyzdef and xxx in xxxdef (all at once, with just one RegEx)?

  • @Alan-Kilborn What if it was abc
    def, xyz
    def and xxx

  • @Scott-Nielson ,

    Anything you put before the \K will be effectively “lookbehind”. Before that \K, you can use any valid regex syntax, which will carry the same meaning. So if you want to match the def, but only if it’s prefaced by abc or xyz, or one-or-more-xes, and with zero or more spaces between it and the def you could look for (?:abc|xyz|x+)\h*\Kdef – this will match the def in abcdef or abc def or abc def (with lots of spaces between, which the forum won’t let me show) or xyz def or xxxxxxx def or xdef or many others.

    (In your actual example of xxx, and without the variable number of spaces, there would have been no reason for the \K, and you could have used a normal lookbehind, because the number of characters could be exactly known by the regex engine: (?<=(abc|xyz|xxx) )def . Thus, I changed the example to something that could match any number of characters, and thus wouldn’t work in a normal lookbehind situation.)

    (I added spaces while writing this up, because your multiple posts and edits were moving in that direction)

  • Examples:


    abc def
    xyz def
    pdq def
    x def
    xx def
    xxx def
    xxxx def
    x       def
    xx      def
    xxx     def
    xxxx    def

  • @PeterJones I want to use the lookbehind RegEx only. Suppose the def in the string I typed above is the only unique code (or whatever it may be called), how to find all that I asked for above using the lookbehind RegEx?

  • @PeterJones Something like (?<!abc)(?<!xyz)(?<!xxx)(?<=def)?

  • @PeterJones I believe your Regex will work but what if I wanted to use the lookbehind RegEx and skip some codes/strings just before a unique code/string?

  • @Scott-Nielson ,

    I am not here to be your personal regex writer and regex debugger, sorry. I gave you general principals. I gave you specific examples. We have linked you to regex documentation. I’m not sure what else you can reasonably expect from us.

    As we said, if the length varies, \K is equivalent to lookbehind. You need to use whatever syntax works for your particular situation. Your brief examples aren’t enough for us to know if they’ll work for your actual needs.

    As a parting help,

    1. I have no idea why you suddenly switched to negative lookbehind. All your descriptions have said “I want to match the def assuming that abc or xyz or xxx come before it”. But the regeex you just proposed says “match def as long as none of abc or xyz or xxx come before it” which is literally the opposite of what you’ve previously asked for.
    2. Why did you put def in a lookbehind when you wanted it to be part of the match itself. From that, you would just want (?<!abc)(?<!xyz)(?<!xxx)def – with the def not in a lookbehind.

    But, to use an analogy: it’s time for you to take off the training wheels and try to learn how to balance and bicycle on your own. You need to learn regular expressions enough that you can do it without constantly relying on us to guess whether or not a regular expression will work for the data that we cannot see that you don’t accurately describe to us.


    Please note: This Community Forum is not a data transformation service; you should not expect to be able to always say “I have data like X and want it to look like Y” and have us do all the work for you. If you are new to the Forum, and new to regular expressions, we will often give help on the first one or two data-transformation questions, especially if they are well-asked and you show a willingness to learn; and we will point you to the documentation where you can learn how to do the data transformations for yourself in the future. But if you repeatedly ask us to do your work for you, you will find that the patience of usually-helpful Community members wears thin. The best way to learn regular expressions is by experimenting with them yourself, and getting a feel for how they work; having us spoon-feed you the answers without you putting in the effort doesn’t help you in the long term and is uninteresting and annoying for us.


    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.

  • @PeterJones OK, thanks a lot. Like you said, I have been experimenting!

Log in to reply