Community
    • Login

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

    Scheduled Pinned Locked Moved General Discussion
    8 Posts 3 Posters 447 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.
    • Robert Or Janet DiebelR
      Robert Or Janet Diebel
      last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • Terry RT
        Terry R
        last edited by Terry R

        @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

        1 Reply Last reply Reply Quote 1
        • Robert Or Janet DiebelR
          Robert Or Janet Diebel
          last edited by

          @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.

          Alan KilbornA Robert Or Janet DiebelR 2 Replies Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @Robert Or Janet Diebel
            last edited by

            @Robert-Or-Janet-Diebel

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

            1 Reply Last reply Reply Quote 0
            • Robert Or Janet DiebelR
              Robert Or Janet Diebel @Robert Or Janet Diebel
              last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • Robert Or Janet DiebelR
                Robert Or Janet Diebel
                last edited by

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

                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Robert Or Janet DiebelR
                  Robert Or Janet Diebel
                  last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn @Robert Or Janet Diebel
                    last edited by

                    @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

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