In in Notepad++/pcre/PHP \K
resets starting point of match. That ability is not found in the .Net flavor of regex.
How do I go about skipping a specified number of characters?
I created a pattern to do a Replace on ^.{15}
, which eliminates the first 15 characters....Am I on the right track, or is there a better way?
In PCRE, Oniguruma, Boost, ICU regex flavors \K
is a kind of a lookbehind construct:
There is a special form of this construct, called
\K
(available since Perl 5.10.0), which causes the regex engine to "keep" everything it had matched prior to the\K
and not include it in$&
. This effectively provides variable-length look-behind. The use of\K
inside of another look-around assertion is allowed, but the behaviour is currently not well defined.
In .NET, \K
is not necessary since it has variable-width (or infinite-width) lookbehind.
(?<=subexpression)
is a positive lookbehind assertion; that is, the character or characters before the current position must match subexpression.
To match a digit after the first 15 any characters, use
(?<=^.{15})\d
See demo
Do not forget that to make the dot match a newline, you need to use RegexOptions.Singleline
.
A note from rexegg.com:
The only two programming-language flavors that support infinite-width lookbehind are .NET (C#, VB.NET, …) and Matthew Barnett's regex module for Python.
And as a bonus: your current requirement does not mean you depend on the infinite width lookbehind. Just use a capturing group:
var s = Regex.Replace("1234567890123456", @"^(.{15})\d", "$1*");
The last 6
will get replaced with *
and the beginning will get restored in the resulting string using the $1
backreference.
Give this a try:
(?<=.{15}).+
Technichally those character are not skipped, as they are consumed and considered part of the match. If you would like to skip the characters, then you need a look behind
(?<=(.){15})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With