Regex - \K pattern alternatives
-
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?
-
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
\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 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
.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 lettert
in the string twisty streets. This syntax should work with.NET
and does not work with ourBoost
regex engine. Whereas the similar syntax(?<=is|ee)t
, with alternatives of the same length, is correct withBoost
! -
(?<=s\w{1,7})t
matches only the fourth lettert
in the string twisty streets. This syntax should work with.NET
and does not work with ourBoost
regex engine. Whereas the similar syntax(?<=s\w{4})t,
, with a fix number of repetitions, is correct withBoost
! -
(?<=s\w+)t
matches only the fourth lettert
in the string twisty streets. This syntax should work with.NET
and does not work with ourBoost
regex engine. Whereas the syntaxs\w+\Kt
, using the\K
feature, is correct withBoost
! -
(\w).+(?<=\1)
, with a back-reference inside the look-behind, matchestwisty street
in the string twisty streets. This syntax should work with.NET
and does not work with ourBoost
regex 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
.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 lettert
in the string twisty streets. Seemingly, this syntax is not allowed in.NET
regex engine but does work with ourBoost
regex engine ! Note that this example is trivial because we can use, instead, both with.NET
andBoost
, 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
-