Regex - \K pattern alternatives
-
hello. I need some alternatives for
\Kpattern, 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.*\RCan anyone help me?
-
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.
-
Hi @vasile-caraus and All,
Indeed the
\Kconstruction is not allowed in.NET. However, from this link, below :https://www.regular-expressions.info/refadv.html
It is said that
.NETsupports full regular expression syntax. So, you do not need the use of the\Kfeature ;-))Once, you’ve clicked on the link, at top of page, under the title Regular Expression Reference: Special Groups, choose the
Boostflavor in the left drop-down list and the.NETflavor in the right drop-down list, then look at the respective columns, on the right of the tableBeware that the different flavors are not listed in alphabetic order
Best Regards,
guy038
-
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.
-
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
.NETsupports full regular expression syntax. So, you do not need the use of the\Kfeature ;-))But I wanted to say :
It is said that
.NETsupports full regular expression syntax within look-behinds. So, you do not need the use of the\Kfeature ;-))Indeed, using the examples of special look-behind constructions, given in :
https://www.regular-expressions.info/refadv.html
-
(?<=is|e)tmatches the second and fourth lettertin the string twisty streets. This syntax should work with.NETand does not work with ourBoostregex engine. Whereas the similar syntax(?<=is|ee)t, with alternatives of the same length, is correct withBoost! -
(?<=s\w{1,7})tmatches only the fourth lettertin the string twisty streets. This syntax should work with.NETand does not work with ourBoostregex engine. Whereas the similar syntax(?<=s\w{4})t,, with a fix number of repetitions, is correct withBoost! -
(?<=s\w+)tmatches only the fourth lettertin the string twisty streets. This syntax should work with.NETand does not work with ourBoostregex engine. Whereas the syntaxs\w+\Kt, using the\Kfeature, is correct withBoost! -
(\w).+(?<=\1), with a back-reference inside the look-behind, matchestwisty streetin the string twisty streets. This syntax should work with.NETand does not work with ourBoostregex engine. Whereas the syntax(\w).+\1, without any look-behind feature, is, of course, correct withBoost!. 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
.NETsyntax,(\w).+(?<=\1), the.+part matches the string wisty street -
With the
Boostsyntax,(\w).+\1, the.+part matches the string wisty stree
-
-
s\Kt, with the\Kfeature ( “Keep text out of the regex match” ), matches the second and third lettertin the string twisty streets. Seemingly, this syntax is not allowed in.NETregex engine but does work with ourBoostregex engine ! Note that this example is trivial because we can use, instead, both with.NETandBoost, 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
-