Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip first nn characters, take the rest with RegEx

Tags:

.net

regex

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?

like image 540
Steve Owens Avatar asked Dec 10 '15 19:12

Steve Owens


3 Answers

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.

like image 70
Wiktor Stribiżew Avatar answered Nov 18 '22 23:11

Wiktor Stribiżew


Give this a try:

(?<=.{15}).+
like image 6
iikkoo Avatar answered Nov 19 '22 00:11

iikkoo


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})
like image 2
swestner Avatar answered Nov 19 '22 00:11

swestner