• Login
Community
  • Login

regexp help: lookahead and lookbehind with spaces

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
12 Posts 4 Posters 731 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.
  • P
    patrickdrd
    last edited by patrickdrd Mar 3, 2022, 10:21 AM Mar 3, 2022, 10:18 AM

    hello everyone,
    I would like your help with a regexp,
    I want to join 2 regexp in 1, i.e.:

    (f1|mls|ucl)\s*fantasy|fantasy\s*(f1|mls|ucl)
    

    in one statement (both ways),
    I mean I would like my regexp to match
    “f1 fantasy” (space in between, without quotes) and “fantasy f1”
    but not “f1 asasdasda fantasy”

    this is a generic regexp question,
    also how do I do lookahead and lookbehind in npp,

    thanks

    A 1 Reply Last reply Mar 3, 2022, 12:18 PM Reply Quote 0
    • A
      Alan Kilborn @patrickdrd
      last edited by Mar 3, 2022, 12:18 PM

      @patrickdrd

      “f1 fantasy” (space in between, without quotes) and “fantasy f1”

      (?:(f1)|(fantasy)) (?(1)(?2)|(?1))

      also how do I do lookahead and lookbehind in npp

      Decribed in the N++ user manual.

      P 1 Reply Last reply Mar 3, 2022, 12:22 PM Reply Quote 3
      • P
        patrickdrd @Alan Kilborn
        last edited by Mar 3, 2022, 12:22 PM

        @alan-kilborn thanks but I would like f1fantasy matched as well

        A 1 Reply Last reply Mar 3, 2022, 12:26 PM Reply Quote 0
        • A
          Alan Kilborn @patrickdrd
          last edited by Mar 3, 2022, 12:26 PM

          @patrickdrd said in regexp help: lookahead and lookbehind with spaces:

          but I would like f1fantasy matched as well

          Hmm, the target moves…

          I think you can figure out how to change my regex do that, given your first guess at it. :-)

          P 1 Reply Last reply Mar 3, 2022, 12:31 PM Reply Quote 2
          • P
            patrickdrd @Alan Kilborn
            last edited by patrickdrd Mar 3, 2022, 12:31 PM Mar 3, 2022, 12:31 PM

            @alan-kilborn \s* is the 0 or more spaces but I can’t figure out where to attach it to, I tried all places and it doesn’t work

            I also got a couple more conditions to check for like mls and ucl

            P 1 Reply Last reply Mar 3, 2022, 1:04 PM Reply Quote 0
            • P
              patrickdrd @patrickdrd
              last edited by Mar 3, 2022, 1:04 PM

              ok got it,

              (?:(f1|ucl|mls)|(fantasy))\s*(?(1)(?2)|(?1))
              
              1 Reply Last reply Reply Quote 3
              • G
                guy038
                last edited by guy038 Mar 4, 2022, 4:07 AM Mar 4, 2022, 4:04 AM

                Hello, @patrickdrd, @alan-kilborn and All,

                Actually, two solutions are possibles :

                Regex A : SEARCH (?-i:(f1|ucl|mls)|(fantasy))\s*(?(1)(?2)|(?1))

                Regex B : SEARCH (?-i:(f1|ucl|mls)|(fantasy))\x20*(?(1)(?2)|(?1))

                It does not match exactly the same occurrences !


                • Paste the text below in a new tab :
                f1fantasy
                f1 fantasy
                f1      fantasy
                uclfantasy
                ucl fantasy
                ucl            fantasy
                mlsfantasy
                mls fantasy
                mls    fantasy
                
                fantasyf1
                fantasy f1
                fantasy                f1
                fantasyucl
                fantasy ucl
                fantasy          ucl
                fantasymls
                fantasy mls
                fantasy                       mls
                ============================================================
                Match with \x{000D}\x{000A} (CRLF) in between :
                f1
                fantasy
                
                Match with \x{000A} (LF) in between :
                f1
                fantasy
                
                Match with \x{000D} (CR) in between :
                f1
                fantasy  
                
                Match with \x{0009} (TABULATION) in between :
                fantasy	f1
                
                Match with \x{0011} ( VERTICAL TABULATION ) in between :
                f1fantasy
                
                Match with \x{0085} ( NO-BREAK SPACE ) in between :
                fantasy f1
                
                with a Multi-lignes MIX of these SPECIFIC chars in between :
                fantasy			
                		            f1
                
                • Open the Mark dialog ( Ctrl + M )

                • Tick only the Purge for each search and Wrap around options

                • Click on the Mark All button

                As you can see, the regex A, in addition to match usual space chars, also matches a lot a “SPACE” characters !

                => After the line of equal signs, some matches, possibly multi lines, occurred !

                So the regex B, more rigorous, just matches zero or more \x20 chars ;-))

                => The part, after the equal signs is not detected at all !

                If your different words may be present in any case, simply replace the -i in-line modifier, within the non-capturing group, by the i modifier !

                Best regards,

                guy038

                P 1 Reply Last reply Mar 4, 2022, 7:36 AM Reply Quote 2
                • P
                  patrickdrd @guy038
                  last edited by patrickdrd Mar 4, 2022, 7:38 AM Mar 4, 2022, 7:36 AM

                  @guy038 I prefere the first approach,
                  because I want it to match tabs as well,
                  also \n shouldn’t be a problem because I’m matching single line items

                  N 1 Reply Last reply Mar 4, 2022, 10:20 AM Reply Quote 1
                  • N
                    Neil Schipper @patrickdrd
                    last edited by Mar 4, 2022, 10:20 AM

                    @patrickdrd and @guy038,

                    In much the same way that Goldilocks found Papa Bear’s bed too hard, and Mama Bear’s bed too soft, but Baby Bear’s bed just right …

                    in the present context, surely \s is too promiscuous, and \x20 is too brittle, while \h is just right!

                    P 1 Reply Last reply Mar 4, 2022, 2:16 PM Reply Quote 4
                    • G
                      guy038
                      last edited by guy038 Mar 4, 2022, 1:11 PM Mar 4, 2022, 1:10 PM

                      Hi, @patrickdrd, @alan-kilborn, @neil-schipper and All,

                      @neil-schipper is perfectly right ! This third solution (?-i:(f1|ucl|mls)|(fantasy))\h*(?(1)(?2)|(?1)) is just what you need as \h* will match any combination, possibly null, of Space, Tabulation and/or No-Break Space char(s) !

                      BR

                      guy038

                      1 Reply Last reply Reply Quote 2
                      • P
                        patrickdrd @Neil Schipper
                        last edited by Mar 4, 2022, 2:16 PM

                        @neil-schipper I still tend to think that \s is slightly better because why not match “accidental” carriage returns/ line feeds?

                        which is higher? the possibility of an “accidental” carriage return/ line feed or a “next line mismatch”?
                        I don’t know if you understand, but I think the first is higher

                        N 1 Reply Last reply Mar 4, 2022, 3:54 PM Reply Quote 2
                        • N
                          Neil Schipper @patrickdrd
                          last edited by Mar 4, 2022, 3:54 PM

                          @patrickdrd said in regexp help: lookahead and lookbehind with spaces:

                          why not match “accidental” carriage returns/ line feeds?

                          I don’t know enough about your data or the intended purpose of the pair matching to say either way, but I will say that, since you are gathering up either ab or ba pairs, then, a small error can make every subsequent pair a different pair from what would have matched without the error.

                          It’s up to you to think through whether this harms what you’re trying to accomplish, and if it does, to try to devise a strategy that could detect a malformed pair, and maybe skip it and then resync and pick up all subsequent pairs with desired grouping.

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