Consider such code:
Match match = new Regex("^bar").Match(some_string,3);
I would like to match some_string but not from the beginning of it, but starting from given position. The catch is, I would like to make a match exactly from that position, thus the anchor.
Unfortunately, it does not work. MS regex does not work with chunks of the string as strings on their own, so ^ matches only the one and true beginning of the string not of starting position.
The easy workaround is to write this in such way:
Match match = new Regex("^bar").Match(some_string.Substring(3));
The downsize — speed (i.e. lack of it).
So my question is this — how to match anchored regex in the middle of the string, fast?
.NET has a separate anchor for the position where the engine starts (usually the end of the last match, in your case the offset):
\G.So you should be able to use the pattern
@"\Gbar".Source (MSDN)