Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    How to bookmark lines around a line containing a specific expression 'XXX' ?

    General Discussion
    3
    14
    1255
    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.
    • guy038
      guy038 last edited by guy038

      Hi, All,

      So I’ve updated all my previous regexes, in order to bookmark pure empty lines, as well ! ( They do not contain the + quantifier, anymore ! )


      This first table, below, shows how to mark consecutive lines, around a line containing a specific expression XXX. Almost obvious ;-))

          •===•======================================================•===========•==========•===========•
          |   |  REGULAR expression to mark Line X, containing 'XXX' | Nth lines |  Line X  | Mth lines |
          |   |  and Nth lines BEFORE and/or Mth lines AFTER line X  |   BEFORE  |          |   AFTER   |
          •===•======================================================•===========•==========•===========•
          |   | (?-is)^(.*\R){N}(?=.*XXX)                            |    YES    |     NO   |      NO   |
          •---•------------------------------------------------------•-----------•----------•-----------•
          |   | (?-is)^.*XXX.*\R\K(.*\R){M}                          |     NO    |     NO   |     YES   |
          •---•------------------------------------------------------•-----------•----------•-----------•
          | X | (?-is)^(.*\R){N}(?=.*XXX)|.*XXX.*\R\K(.*\R){M}       |    YES    |     NO   |     YES   |
          •---•------------------------------------------------------•-----------•----------•-----------•
          |   | (?-is)^.*XXX.*                    ( TRIVIAL case )   |     NO    |    YES   |      NO   |
          •---•------------------------------------------------------•-----------•----------•-----------•
          |   | (?-is)^(.*\R){N}.*XXX.*                              |    YES    |    YES   |      NO   |
          •---•------------------------------------------------------•-----------•----------•-----------•
          |   | (?-is)^.*XXX.*\R(.*\R){M}                            |     NO    |    YES   |     YES   |
          •---•------------------------------------------------------•-----------•----------•-----------•
          | Y | (?-is)^(.*\R){N}.*XXX.*\R(.*\R){M}                   |    YES    |    YES   |     YES   |
          •===•======================================================•===========•==========•===========•
      

      Notes :

      • You must replace the N and M variables, in the regexes, with an integer, standing for the number of wanted lines, respectively, before and after the specific line X !

      • If you choose N = 0 and/or M = 0, the different regexes still work ! So, with the help of the regexes identified with an, X or Y you can, as well, solve all the described cases ;-))

      • However, if you choose N = 0 and M <> 0, the regex, identified with an X, will not work properly when a criterion XXX begins a line


      In this second table, here are the regexes which does NOT bookmark any Line X, but only the 1 or 2 lines around the Line X :

          •=========================================================•==========•==========•==========•==========•==========•
          |  REGULAR expression, marking ONE or TWO lines, BEFORE   | 2nd line | 1st line |  Line X  | 1st line | 2nd line |
          |  / AFTER a line X, containing 'XXX', except the line X  |  BEFORE  |  BEFORE  |          |   AFTER  |   AFTER  |
          •=========================================================•==========•==========•==========•==========•==========•
          | (?-is)^.*XXX.*\R.*\R\K.*\R                              |     NO   |     NO   |     NO   |     NO   |    YES   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*XXX.*\R\K.*\R                                  |     NO   |     NO   |     NO   |    YES   |     NO   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*XXX.*\R\K.*\R.*\R                              |     NO   |     NO   |     NO   |    YES   |    YES   |
          •=========================================================•==========•==========•==========•==========•==========•
          | (?-is)^.*\R(?=.*XXX)                                    |     NO   |    YES   |     NO   |     NO   |     NO   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R(?=.*XXX)|^.*XXX.*\R.*\R\K.*\R               |     NO   |    YES   |     NO   |     NO   |    YES   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R(?=.*XXX)|^.*XXX.*\R\K.*\R                   |     NO   |    YES   |     NO   |    YES   |     NO   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R(?=.*XXX)|^.*XXX.*\R\K.*\R.*\R               |     NO   |    YES   |     NO   |    YES   |    YES   |
          •=========================================================•==========•==========•==========•==========•==========•
          | (?-is)^.*\R(?=.*\R.*XXX)                                |    YES   |     NO   |     NO   |     NO   |     NO   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R(?=.*\R.*XXX)|^.*XXX.*\R.*\R\K.*\R           |    YES   |     NO   |     NO   |     NO   |    YES   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R(?=.*\R.*XXX)|^.*XXX.*\R\K.*\R               |    YES   |     NO   |     NO   |    YES   |     NO   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R(?=.*\R.*XXX)|^.*XXX.*\R\K.*\R.*\R           |    YES   |     NO   |     NO   |    YES   |    YES   |
          •=========================================================•==========•==========•==========•==========•==========•
          | (?-is)^.*\R.*\R(?=.*XXX)                                |    YES   |    YES   |     NO   |     NO   |     NO   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R.*\R(?=.*XXX)|^.*XXX.*\R.*\R\K.*\R           |    YES   |    YES   |     NO   |     NO   |    YES   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R.*\R(?=.*XXX)|^.*XXX.*\R\K.*\R               |    YES   |    YES   |     NO   |    YES   |     NO   |
          •---------------------------------------------------------•----------•----------•----------•----------•----------•
          | (?-is)^.*\R.*\R(?=.*XXX)|^.*XXX.*\R\K.*\R.*\R           |    YES   |    YES   |     NO   |    YES   |    YES   |
          •=========================================================•==========•==========•==========•==========•==========•
      

      • In the third table, here are the regexes which do bookmark the Line X, as well as 1 or 2 lines around Line X :
          •===•===========================================================•==========•==========•==========•==========•==========•
          |   |  REGULAR expression, marking line X, containing 'XXX',    | 2nd line | 1st line |  Line X  | 1st line | 2nd line |
          |   |   as well as ONE or TWO lines, BEFORE / AFTER line X      |  BEFORE  |  BEFORE  |          |   AFTER  |   AFTER  |
          •===•===========================================================•==========•==========•==========•==========•==========•
          |   |  (?-is)^.*XXX.*                        ( Trivial case )   |     NO   |     NO   |    YES   |     NO   |     NO   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          | X |  (?-is)^.*(?=XXX)|\GXXX.*\R.*\R\K.*\R                     |     NO   |     NO   |    YES   |     NO   |    YES   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          |   |  (?-is)^.*XXX.*\R.*\R                                     |     NO   |     NO   |    YES   |    YES   |     NO   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          |   |  (?-is)^.*XXX.*\R.*\R.*\R                                 |     NO   |     NO   |    YES   |    YES   |    YES   |
          •===•===========================================================•==========•==========•==========•==========•==========•
          |   |  (?-is)^.*\R.*XXX.*                                       |     NO   |    YES   |    YES   |     NO   |     NO   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          | X |  (?-is)^.*\R.*(?=XXX)|\GXXX.*\R.*\R\K.*\R                 |     NO   |    YES   |    YES   |     NO   |    YES   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          |   |  (?-is)^.*\R.*XXX.*\R.*\R                                 |     NO   |    YES   |    YES   |    YES   |     NO   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          |   |  (?-is)^.*\R.*XXX.*\R.*\R.*\R                             |     NO   |    YES   |    YES   |    YES   |    YES   |
          •===•===========================================================•==========•==========•==========•==========•==========•
          |   |  (?-is)^.*\R(?=.*\R.*XXX)|^.*XXX.*                        |    YES   |     NO   |    YES   |     NO   |     NO   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          | X |  (?-is)^.*\R(?=.*\R.*XXX)|^.*(?=XXX)|\GXXX.*\R.*\R\K.*\R  |    YES   |     NO   |    YES   |     NO   |    YES   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          |   |  (?-is)^.*\R(?=.*\R.*XXX)|^.*XXX.*\R.*\R                  |    YES   |     NO   |    YES   |    YES   |     NO   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          |   |  (?-is)^.*\R(?=.*\R.*XXX)|^.*XXX.*\R.*\R.*\R              |    YES   |     NO   |    YES   |    YES   |    YES   |
          •===•===========================================================•==========•==========•==========•==========•==========•
          |   |  (?-is)^.*\R.*\R.*XXX.*                                   |    YES   |    YES   |    YES   |     NO   |     NO   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          | X |  (?-is)^.*\R.*\R.*(?=XXX)|\GXXX.*\R.*\R\K.*\R             |    YES   |    YES   |    YES   |     NO   |    YES   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          |   |  (?-is)^.*\R.*\R.*XXX.*\R.*\R                             |    YES   |    YES   |    YES   |    YES   |     NO   |
          •---•-----------------------------------------------------------•----------•----------•----------•----------•----------•
          |   |  (?-is)^.*\R.*\R.*XXX.*\R.*\R.*\R                         |    YES   |    YES   |    YES   |    YES   |    YES   |
          •===•===========================================================•==========•==========•==========•==========•==========•
      

      Notes :

      • Due to the \G syntax, in the 4 regexes, identified with an X, results are not correct when a criterion XXX begins a line

      • When these regexes are used and that the criterion XXX, does not begin a line, as a side-effect, the only characters, before XXX, will be red-marked. But, luckily, any line, containing XXX, will be correctly bookmarked :-))

      Best Regards,

      guy038

      1 Reply Last reply Reply Quote 2
      • Alan Kilborn
        Alan Kilborn @guy038 last edited by

        @guy038 said:

        …the regex matches a zero length empty string, in any empty line. And, if your try to mark and bookmark these lines nothing is displayed on screen !

        Yes, I suppose that one needs to keep in mind that if a (successful) operation results in no marked text (such as a zero-length match) then as a consequence there is not going to be any bookmarked line either.

        1 Reply Last reply Reply Quote 1
        • NotPad001
          NotPad001 last edited by

          Hi,
          I tried to understand this topic and wonder how to solve the following task:

          Below is a list with various data – and I would like to Mark a ’Bookmark Line’ for all the Data for ’GENDER=MALE’. I want to Mark all lines between the words “STARTDATA” and “ENDDATA” for every found ’GENDER=MALE’ as seen in the Before and After text below.

          As seen below - the 2nd found ’GENDER=MALE’ contains one more extra line between its “STARTDATA” and “ENDDATA”.

          The EXAMPLE USE CASE:

          How the Data looks like BEFORE:

          STARTDATA:
          GENDER=MALE
          NAME=JOE
          AGE=34
          HEIGHT=181
          ENDDATA:
          STARTDATA:
          GENDER=FEMALE
          NAME=MARIA
          AGE=38
          HEIGHT=163
          ENDDATA:
          STARTDATA:
          GENDER=FEMALE
          NAME=DIANA
          AGE=56
          HEIGHT=150
          ENDDATA:
          STARTDATA:
          GENDER=MALE
          NAME=KEVIN
          AGE=21
          HEIGHT=201
          WEIGHT=97
          ENDDATA:

          The requested Data AFTER where I also added the Line Numbers and dots just for highlight.

          1 • STARTDATA:
          2 • GENDER=MALE
          3 • NAME=JOE
          4 • AGE=34
          5 • HEIGHT=181
          6 • ENDDATA:
          7 STARTDATA:
          8 GENDER=FEMALE
          9 NAME=MARIA
          10 AGE=38
          11 HEIGHT=163
          12 ENDDATA:
          13 STARTDATA:
          14 GENDER=FEMALE
          15 NAME=DIANA
          16 AGE=56
          17 HEIGHT=150
          18 ENDDATA:
          19 • STARTDATA:
          20 • GENDER=MALE
          21 • NAME=KEVIN
          22 • AGE=21
          23 • HEIGHT=201
          24 • WEIGHT=97
          25 • ENDDATA:

          Thanks a lot,

          1 Reply Last reply Reply Quote 0
          • guy038
            guy038 last edited by guy038

            Hello, @notpad001, and All,

            First, thanks for your exact description of your problem :-))

            Ah… I see ! My regexes described in previous posts, of this topic, are useless, in your case, because you may not have the same number of lines, between your criterion GENDER= MALE and the end of the current section ENDDATA:. The previous regex examples are line-oriented ( because of the (?-is) syntax ) and catch an exact amount of lines !

            In your case, we must use the no-single line concept, so the syntax (?s-i), which allows the regex engine to consider that the dot regex symbol . matches absolutely any single character, including EOL chars. In other words, the regex will be able to match any multi-lines range of text !


            Then, if we assume that the line GENDER=..... comes right after the line STARTDATA:, the following regex could be the right one !

            SEARCH (?s-i)^STARTDATA:\RGENDER=MALE.+?^ENDDATA:

            Notes :

            • I suppose that the search is sensible to case. If NOT, simply change the beginning of the regex as (?si)

            • Now, it looks for any line STARTDATA:, beginning a line ( ^ ), followed with its line-break ( \R ) and the line GENDER=MALE

            • Then the part .+? tries to match the shortest non-null range of any character, till the line ENDDATA:, beginning a line ( ^ )

            • All lines, even partially, involved in the match, are then bookmarked


            Remark : In case the line GENDER=..... is preceded with a fix or variable amount of line(s), even empty, use the generic regex :

            SEARCH (?s-i)^STARTDATA:\R(?-s:.*\R){N,M} GENDER=MALE.+?^ENDDATA:

            For instance, if we suppose this initial text :

            STARTDATA:
            11111
            
            3333
            GENDER=MALE
            NAME=JOE
            AGE=34
            HEIGHT=181
            ENDDATA:
            STARTDATA:
            GENDER=FEMALE
            NAME=MARIA
            AGE=38
            HEIGHT=163
            ENDDATA:
            STARTDATA:
            GENDER=MALE
            NAME=JOHN
            AGE=37
            HEIGHT=197
            WEIGHT=105
            ENDDATA:
            STARTDATA:
            GENDER=FEMALE
            NAME=DIANA
            AGE=56
            HEIGHT=150
            ENDDATA:
            STARTDATA:
            1111
            2222
            GENDER=MALE
            NAME=KEVIN
            AGE=21
            HEIGHT=201
            WEIGHT=97
            ENDDATA:
            

            The regex :

            (?s-i)^STARTDATA:\R(?-s:.*\R){0,3}GENDER=MALE.+?^ENDDATA:

            correctly mark the sections, containing the line GENDER=MALE, if it exists from 0 to 3 lines between a line STARTDATA: and its line GENDER=MALE

            Note :

            • The part (?-s: begins a non-capturing group and any dot regex symbol, inside this non-capturing group, will match a single standard char ( not EOL ones )

            • The part {0,3} matches from 0 to 3 complete lines, with their line-break(s) .*\R, located before the line GENDER=MALE

            Best Regards

            guy038

            1 Reply Last reply Reply Quote 1
            • NotPad001
              NotPad001 last edited by

              Thanks guy038 and this was very good help –and looks promising! - but I have a dilemma:
              For GENDER=MALE I used the regex (?s-i)^STARTDATA:\R(?-s:.*\R){0,3}GENDER=MALE.+?^ENDDATA:
              => This correctly Marked row 1-6 (Great)
              However: Row 19-25 was not Marked

              For GENDER=FEMALE I used the regex (?s-i)^STARTDATA:\R(?-s:.*\R){0,3}GENDER=FEMALE.+?^ENDDATA:
              => This correctly Marked Two set of FEMALE rows 7-12 and 13-18 (Great)

              The dilemma is that the file I have contains a lot of data and I want to be able to filter out all GENDER=MALE date. (Row 1-6 and 19-25)
              I toggled:
              Bookmark line ticked
              Purge for each search
              Wrap around
              Regular expression

              Thanks a lot!

              GENDER=MALE.jpg

              GENDER=FEMALE.jpg

              1 Reply Last reply Reply Quote 0
              • guy038
                guy038 last edited by guy038

                Hi NotPad001 and All,

                Ah ! I understood why the regex does not match the 2nd section with GENDER=MALE Don’t you see it ? Well, seemingly, from your picture, the last line ( line 25 ) does not end with a colon character ! And it’s obvious that searching from GENDER=FEMALE sections worked perfectly right ;-))

                So the correct regex to process is :

                SEARCH (?s-i)^STARTDATA:\R(?-s:.*\R){0,3}GENDER=MALE.+?^ENDDATA:?

                I just added a regex question mark symbol ( ? ), which is a shorthand of the {0,1} quantifier, at the end of the regex

                BTW, note that, if the line GENDER=.... is located right after the STARTDATA: line, you may simply use the shorter regex :

                SEARCH (?s-i)^STARTDATA:\RGENDER=MALE.+?^ENDDATA:?


                Now, there are a lot of methods to filter out your text : Let’s suppose that you want to keep sections with GENDER=MALE, only :

                • First, you could use an option from the Bookmark menu ( Search > Bookmark )

                  • Bookmark sections containing GENDER=MALE, with the appropriate regex and, then, use the option Search > Bookmark > Remove Unmarked lines

                  • Bookmark sections containing GENDER=FEMALE, with the appropriate regex and, then, use the option Search > Bookmark > Remove Bookmarked lines

                • Secondly, you could use one of the two regex S/R, below, ( Ctrl + H ), directly, searching for GENDER=FEMALE sections and clicking on the Replace All button :

                  • SEARCH (?s-i)^STARTDATA:\RGENDER=FEMALE.+?^ENDDATA:?\R?

                  • REPLACE Leave EMPTY

                  • SEARCH (?s-i)^STARTDATA:\R(?-s:.*\R){M,N}GENDER=FEMALE.+?^ENDDATA:?\R?

                  • REPLACE Leave EMPTY

                Remark that, in order to delete the entire lines ENDDATA:, we need to add the part \R? at the end of the regexes to match the line-break of lines ENDDATA(:) too, which is optional, in case that the very last line of your file is not followed with a line-break !

                Best regards,

                guy038

                1 Reply Last reply Reply Quote 1
                • NotPad001
                  NotPad001 last edited by

                  Thanks guy038 – you are spot on!
                  Now it works very well after I included the missing : on the last row in my example. My fault – and I will be more observant! Also thanks for the additional information. You know your stuff!!

                  Related questions on this topic:
                  Is it possible to include wildcard search criteria such as instead of searching for GENDER=MALE you only search for NDER=MA in a similar way as using the star (*) in front of NDER=MA when searching for things in Excel. The requested result is to Bookmark Line the same lines as in the example that you guy038 solved.

                  Because: I noticed that in case I have spaces in front of the GENDER=MALE – I cannot get it to work. Do you have any ideas how to solve that use case?

                  Example: GENDER=MALE has 2 spaces in front of it – but the number of spaces can vary. Ideally I would like to use the wildcard approach as well in the cases where there are spaces in front of the text.

                  Also: In case the lines STARTDATA: and ENDDATA: also have empty Spaces in front of it – can that be captured as well? The use case is to mark the same Lines as successfully marked in the example that you guy038 fixed.

                  Thanks a lot!

                  Alan Kilborn 1 Reply Last reply Reply Quote 0
                  • Alan Kilborn
                    Alan Kilborn @NotPad001 last edited by

                    @NotPad001 said in How to bookmark lines around a line containing a specific expression 'XXX' ?:

                    wildcard search criteria

                    You should pay closer attention.
                    @guy038 is attempting to educate you about “regular expressions”.
                    To even speak of “wildcard search” in the same breath is blasphemous.

                    1 Reply Last reply Reply Quote 1
                    • guy038
                      guy038 last edited by guy038

                      Hello, @notpad001 and All,

                      No problem ! We just have to add the regex \h*, matching any range, even null, of horizontal blank characters, so, mainly, the tab and space chars, at every beginning of line of the regex

                      So, in case you want to keep sections, containing the GENDER=MALE string, this would result in the *new regex syntaxes, below :

                      • SEARCH (?s-i)^\h*STARTDATA:\R\h*GENDER=FEMALE.+?^\h*ENDDATA:?\R?

                      • REPLACE Leave EMPTY

                      OR

                      • SEARCH (?s-i)^\h*STARTDATA:\R(?-s:.*\R){M,N}\h*GENDER=FEMALE.+?^\h*ENDDATA:?\R? ( Do not forget to replace M and N variables with true integers ! )

                      • REPLACE Leave EMPTY

                      Test, for example, the regex (?s-i)^\h*STARTDATA:\R\h*GENDER=FEMALE.+?^\h*ENDDATA:?\R? against the text, below :

                          STARTDATA:
                              GENDER=MALE
                          NAME=JOE
                      		AGE=34
                          HEIGHT=181
                      				ENDDATA:
                          STARTDATA:
                          GENDER=FEMALE
                      	NAME=MARIA
                          AGE=38
                          HEIGHT=163
                              ENDDATA:
                      	    STARTDATA:
                          GENDER=FEMALE
                      	NAME=DIANA
                          AGE=56
                      				    HEIGHT=150
                          ENDDATA:
                      	STARTDATA:
                          GENDER=MALE
                                      NAME=KEVIN
                          AGE=21
                          HEIGHT=201
                          WEIGHT=97
                      	ENDDATA:
                      

                      You should get the text :

                          STARTDATA:
                              GENDER=MALE
                          NAME=JOE
                      		AGE=34
                          HEIGHT=181
                      				ENDDATA:
                      	STARTDATA:
                          GENDER=MALE
                                      NAME=KEVIN
                          AGE=21
                          HEIGHT=201
                          WEIGHT=97
                      		ENDDATA:
                      

                      And, in order to delete any blank character, after the first N characters of each line :

                      • Use the Edit > Blank Operations > TAB to Space menu option to replace each tab char with its appropriate number of space character(s)

                      • Secondly, use this generic regex S/R, which will delete any space char, after the first N : characters of the each line

                        • SEARCH ^\x20{N}\K\x20+ ( we’ll use, in our case, the real regex ^\x20{4}\K\x20+ )

                        • REPLACE Leave EMPTY

                      After clicking on the Replace All button, exclusively, you’re left with that expected result :

                          STARTDATA:
                          GENDER=MALE
                          NAME=JOE
                          AGE=34
                          HEIGHT=181
                          ENDDATA:
                          STARTDATA:
                          GENDER=MALE
                          NAME=KEVIN
                          AGE=21
                          HEIGHT=201
                          WEIGHT=97
                          ENDDATA:
                      

                      Cheers,

                      guy038

                      1 Reply Last reply Reply Quote 2
                      • guy038
                        guy038 last edited by

                        Hi, @notpad001, @alan-kilborn,

                        Alan, I wouldn’t say it is blasphemous, but rather inappropriate !

                        BR

                        guy038

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post
                        Copyright © 2014 NodeBB Forums | Contributors