Community
    • Login

    Regex - \K pattern alternatives

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 4 Posters 2.2k 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.
    • Vasile CarausV
      Vasile Caraus
      last edited by

      hello. I need some alternatives for \K pattern, because \K isn’t currently supported in .NET Regular expressions. For example in this regex:

      (?s).*?\K(MATCH FIRST PART).*(MATCH SECOND PART)

      or here

      (?-s)\A(.*\R){4}\K.*\R

      Can anyone help me?

      1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones
        last edited by

        because \K isn’t currently supported in .NET Regular expressions

        Then you need to find the documentation on .NET Regular Expression syntax(or a forum dedicated to those), and research there. Some of the docs linked in the regex faq may be of help to you. A quick google search turned up https://docs.microsoft.com/en-us/dotnet/standard/base-types/backtracking-in-regular-expressions, which includes the (?<=subexpression) lookbehind syntax, but I don’t know whether .NET allows variable-length lookbehind or not; if not, and if \K doesn’t work, then I don’t know (and am not going to spend more than one quick google for an off-topic help.

        You are asking us a “cookie baking” question. We’re happy to help with regular expressions in Notepad++ (ie, focused on Boost regex syntax), since that helps you use the Notepad++ to accomplish your job. But asking generic regex expressions (like how to accomplish variable-length lookbehind) is more asking for help in perfecting your cookie-baking recipe, rather than asking how to type your recipe, or search the recipe.

        There may be someone here who knows the right answer off the top of their head, but I’m not that someone. And really, it’s an off-topic question.

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

          Hi @vasile-caraus and All,

          Indeed the \K construction is not allowed in .NET. However, from this link, below :

          https://www.regular-expressions.info/refadv.html

          It is said that .NET supports full regular expression syntax. So, you do not need the use of the \K feature ;-))

          Once, you’ve clicked on the link, at top of page, under the title Regular Expression Reference: Special Groups, choose the Boost flavor in the left drop-down list and the .NET flavor in the right drop-down list, then look at the respective columns, on the right of the table

          Beware that the different flavors are not listed in alphabetic order

          Best Regards,

          guy038

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

            I don’t think that I understand @guy038’s reply at all. But that’s OK because I agree with @PeterJones – this is too off-topic to consider.

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

              Hi @vasile-caraus, @peterjones, @alan-kilborn and All,

              First, Alan I apologize because I forgot a part of a sentence, in my previous post ! I said :

              It is said that .NET supports full regular expression syntax. So, you do not need the use of the \K feature ;-))

              But I wanted to say :

              It is said that .NET supports full regular expression syntax within look-behinds. So, you do not need the use of the \K feature ;-))

              Indeed, using the examples of special look-behind constructions, given in :

              https://www.regular-expressions.info/refadv.html

              • (?<=is|e)t matches the second and fourth letter t in the string twisty streets. This syntax should work with .NET and does not work with our Boost regex engine. Whereas the similar syntax (?<=is|ee)t, with alternatives of the same length, is correct with Boost !

              • (?<=s\w{1,7})t matches only the fourth letter t in the string twisty streets. This syntax should work with .NET and does not work with our Boost regex engine. Whereas the similar syntax (?<=s\w{4})t, , with a fix number of repetitions, is correct with Boost !

              • (?<=s\w+)t matches only the fourth letter t in the string twisty streets. This syntax should work with .NET and does not work with our Boost regex engine. Whereas the syntax s\w+\Kt, using the \K feature, is correct with Boost !

              • (\w).+(?<=\1), with a back-reference inside the look-behind, matches twisty street in the string twisty streets. This syntax should work with .NET and does not work with our Boost regex engine. Whereas the syntax (\w).+\1, without any look-behind feature, is, of course, correct with Boost !. In the both cases, assuming the implicit (?-s) modifier, it matches the longest zone of standard characters between a same starting and ending word character ! However, note a subtle difference :

                • With the .NET syntax, (\w).+(?<=\1), the .+ part matches the string wisty street

                • With the Boost syntax, (\w).+\1, the .+ part matches the string wisty stree

              • s\Kt, with the \K feature ( “Keep text out of the regex match” ), matches the second and third letter t in the string twisty streets. Seemingly, this syntax is not allowed in .NET regex engine but does work with our Boost regex engine ! Note that this example is trivial because we can use, instead, both with .NET and Boost, the regex (?<=s)t, which uses a simple look-behind structure ;-))

              I hope, Alan, that these additional explanations will shed some light on this matter ;-))

              Best Regards,

              guy038

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