RegEx to look for XX in a list of lines. if found print the line before, and the line with XX



  • I have a list of lines. I wish to output all lines with a certain pattern in them, plus the line before each line with the pattern.
    All help much appreciated!



  • @Robert-Or-Janet-Diebel said in RegEx to look for XX in a list of lines. if found print the line before, and the line with XX:

    I wish to output all lines with a certain pattern in them, plus the line before each line with the pattern.

    You would use the bookmark feature, as well as marking them. This then allows for a copy of all marked lines to another tab from which you could print the lines.

    So the regex to mark would be: (?-s).+\R.*\Qxx\E.*(\R|\z) This is inserted into the “Mark” tab and the “bookmark” box is also ticked. Then hit the “Mark All” button. From there you will see some lines (in pairs) marked with a blue circle at the left margin (that’s the default icon). Confirm if you need to that it’s worked as planned, then use the Copy bookmarked lines option (under Search, Bookmark).

    Note I have included the xx you refer to inside of 2 meta characters, namely \Q and \E. This allows you to change the xx characters to anything without possibly including any other meta characters and causing the regex to possibly fail.

    Have a try and please come back with your results. If problems occur, let us know and we could try and change the code to suit, we would need to know where it failed.

    If going to be using regex on a regular basis we’d prefer you try to figure out the code first, even if it doesn’t work. Show us your workings and we generally look more favourably on some who has attempted to try something. Our FAQ section includes references to regular expressions (regex) and other useful information to help you learn.

    Terry



  • @Terry-R said in RegEx to look for XX in a list of lines. if found print the line before, and the line with XX:

    You would use the bookmark feature, as well as marking them. This then allows for a copy of all marked lines to another tab from which you could print the lines.
    So the regex to mark would be: (?-s).+\R.\Qxx\E.(\R|\z) This is inserted into the “Mark” tab and the “bookmark” box is also ticked. Then hit the “Mark All” button. From there you will see some lines (in pairs) marked with a blue circle at the left margin (that’s the default icon). Confirm if you need to that it’s worked as planned, then use the Copy bookmarked lines option (under Search, Bookmark).
    Note I have included the xx you refer to inside of 2 meta characters, namely \Q and \E. This allows you to change the xx characters to anything without possibly including any other meta characters and causing the regex to possibly fail.
    Have a try and please come back with your results. If problems occur, let us know and we could try and change the code to suit, we would need to know where it failed.
    If going to be using regex on a regular basis we’d prefer you try to figure out the code first, even if it doesn’t work. Show us your workings and we generally look more favourably on some who has attempted to try something. Our FAQ section includes references to regular expressions (regex) and other useful information to help you learn.
    Terry

    I am searching for lines with “{\#” so RegEx = “{\\#” for the “XX”
    The file looks like this:

    Line 77: 05052-Ro10.8 <2021-07-18 07:47>
    Line 147: 05053-Ro10.9 <2021-07-20 10:07>
    Line 164: taking {\#Ro 10:9\} as the content of the “word of faith.”
    Line 208: 05054-Ro10.10 <2021-07-18 07:46>
    Line 249: 05055-Ro10.11 <2021-07-18 07:51>
    Line 309: 05056-Ro10.12 <2021-07-18 07:51>
    Line 357: 05057-Ro10.13 <2021/04/16 13:31>
    Line 422: 05058-Ro10.14 <2021-07-18 07:50>
    Line 474: 05059-Ro10.15 <2021-07-18 07:49>
    Line 522: 05060-Ro10.16 <2021-07-18 07:54>
    Line 582: 05061-Ro10.17 <2021/04/16 14:08>
    Line 623: 05062-Ro10.18 <2021-07-18 07:53>
    Line 639: uttereth speech, and night unto night sheweth knowledge” {\#Ps
    Line 699: 05063-Ro10.19 <2021/04/16 14:27>
    Line 739: 05064-Ro10.20 <2021/04/16 15:17>
    So I would like to find Line 147 & 164, then Line 623 & 639; the line with “{\#” and the line before it.
    (?-s).+\R.\Q{\\#\E.(\R|\z) did not mark anything. I will continue trying, but all help much appreciated.



  • @Robert-Or-Janet-Diebel

    (?-s).+\R.*\Q{\#\E.*(\R|\z)



  • @Robert-Or-Janet-Diebel
    Correction: I thought maybe between \Q and \E I would try not doubling the “” and it marked some lines.
    Still testing, but thanx a bunch.



  • When I post, the number of back-slashes I enter is cut in half



  • Looks like it worked. This saved a pile of work! Thanx!



  • @Robert-Or-Janet-Diebel said in RegEx to look for XX in a list of lines. if found print the line before, and the line with XX:

    When I post, the number of back-slashes I enter is cut in half

    Yep, use this to avoid that situation:

    a72b9940-5f8f-4884-96e0-ee0caca434c7-image.png


Log in to reply