Tapatalk

Find text x followed by text y up to 10 lines later

Find text x followed by text y up to 10 lines later

344
MasterMaster
344

    Jul 23, 2009#1

    Hi all,

    I try to create a Perl regexp search to allocate the spots in files that contain a line with the string

    Code: Select all

    ".ObjekteListe().Rows"
    that is NOT followed by

    Code: Select all

    "obj.ObjektFuerBesicherung"
    anywhere in the next 10 lines.

    Can this be done with one regexp? Or do I need to write a fat macro?

    Thanks for any tips.

    Bego
    Normally using all newest english version incl. each hotfix. Win 10 64 bit

    236
    MasterMaster
    236

      Jul 23, 2009#2

      Let's try

      Code: Select all

      "\.ObjekteListe\(\)\.Rows".*\r\n(?!(?:(?:(?!"obj\.ObjektFuerBesicherung").)*$\r\n){10})
      No time right now for an explanation, will follow up if the regex works as desired :)

      Cheers,
      Tim

      344
      MasterMaster
      344

        Jul 23, 2009#3

        Hi Tim !

        I tried it out but it did not find anything. So I reduced the problem a bit and cut out the " that I THINK are wrong and came to something like this:

        Code: Select all

        AAAAA.*\r\n(?!(?:(?:(?!XXXXX).)*$\r\n){10})
        I tried with these test lines and found both AAAAA lines instead of only the first occurrence in line 2:

        Code: Select all

          hkjh lskd hkghjjgjlks lhdhd 
        dfdg  AAAAA fdg d  FIND THIS LINE BECAUSE NO 5-X NEAR
        blibla
        blubb sdflökjsdaf iobjekt 
        blubb besicherung
        sdkfj sdlkjaflöksja öskfdj sk
        blibla
        blubb sdflökjsdaf iobjekt 
        blubb besicherung
        sdkfj sdlkjaflöksja öskfdj sk
        blibla
        blubb sdflökjsdaf iobjekt 
        blubb besicherung
        sdkfj sdlkjaflöksja öskfdj sk
        blibla
        blubb sdflökjsdaf iobjekt 
        blubb besicherung
        sdkfj sdlkjaflöksja öskfdj sk
           dfdg  AAAAA fdg d  DO NOT FIND THIS LINE, 5 - x is near
        blibla
        blubb sdflökjsdaf iobjekt 
        blubb besicherung
        sdkfj sdlkjaflöksja öskfdj sk
        g klfsdglskh XXXXX d gh jh
        blibla
        blubb sdflökjsdaf iobjekt
        We are near ...

        Best regards, Bego
        Normally using all newest english version incl. each hotfix. Win 10 64 bit

        236
        MasterMaster
        236

          Jul 23, 2009#4

          Ah, you're right. There was a "double negation" that screwed it up. How about

          Code: Select all

          AAAAA.*\r\n(?=(?:^(?:(?!XXXXX).)*$\r\n){10})
          This has one drawback: If there are less than 10 lines after AAAAA until end of file, it won't match even if there's no XXXXX in those lines.

          344
          MasterMaster
          344

            Jul 23, 2009#5

            End of file drawback won't really mind.
            But still this one finds both AAAAA ...

              Jul 23, 2009#6

              STOP !!!

              I just figured out that I used the wrong hard disc having 14.20 on it.
              It works well with 15.10.0.1017 which I normally work on.

              Thanks Tim!
              Normally using all newest english version incl. each hotfix. Win 10 64 bit

              236
              MasterMaster
              236

                Jul 24, 2009#7

                The funny thing is that I think I found a bug in RegexBuddy with this regex. It highlights the second AAAAA although it shouldn't, and when I debug the regex, the debugger reports a failed match attempt. Will have to dig further into this... (and maybe find out a more elegant solution)?

                Tim

                  Aug 11, 2009#8

                  There actually was a bug in RegexBuddy which is now fixed. So thanks again for providing us with that interesting problem :)

                  344
                  MasterMaster
                  344

                    Nov 26, 2018#9

                    Hi everybody,

                    I don't know what I do wrong, but I need the opposite of what I needed some years ago. Find text AAAAA only when in the next 10 lines is a BBBBB.
                    I could paste the examples I tried, but it could lead you in the wrong direction. So I ask without my examples.

                    Code: Select all

                    lala
                    xyz AAAAA abc
                    bla
                    blubb
                    this is BBBBB so the AAAAA above should be found
                    ....
                    yxx AAAAA cmvnc
                    line 1
                    line 2
                    line 3
                    line 4
                    line 5
                    line 6
                    line 7
                    line 8
                    line 9
                    line 10
                    line 11
                    line 12
                    this is again BBBBB but AAAAA above shall no more be found
                    Any help appreciated.
                    Normally using all newest english version incl. each hotfix. Win 10 64 bit

                    19176
                    MasterMaster
                    19176

                      Nov 26, 2018#10

                      Hi Bego,

                      this should work:

                      Code: Select all

                      \bAAAAA\b(?=(?:.*$(?!(?:\r\n(?:(?!\bBBBBB\b).)*+){11})(?=(?:\r\n.*){11}))|(?:.*$(?!(?:\r\n.*){11})(?=(?s:.*)\bBBBBB\b)))
                      BR, Fleggy

                      EDIT 1: Here is the "opposite" pattern - AAAAA followed by 10 lines not containing BBBBB (or less if there is less than 10 lines after AAAAA) - in another word your original problem

                      Code: Select all

                      \bAAAAA\b(?=(?:.*$(?=(?:\r\n(?:(?!\bBBBBB\b).)*+){11}))|(?:.*$(?!(?:\r\n.*){11})(?!(?s:.*)\bBBBBB\b)))
                      EDIT 2: Some polishing and fixing the near EOF situations.

                      344
                      MasterMaster
                      344

                        Nov 26, 2018#11

                        Oh yes.  😂

                        Thanks man!

                        rds Bego
                        Normally using all newest english version incl. each hotfix. Win 10 64 bit