tags:

views:

41

answers:

2

Suppose I want to match "abc" within the string s only if it occurs exactly at index n.

int n = 2;
Console.WriteLine(new Regex("abc").IsMatch("01abc", n)); // true
Console.WriteLine(new Regex("abc").IsMatch("0123abc", n)); // true (but want false)
Console.WriteLine(new Regex("^abc").IsMatch("01abc", n)); // false (but want true)

Seems that the only way to achieve this without using Substring on the input is something like this:

var match = new Regex("abc").Match("0123abc", n);
Console.WriteLine(match.Success && match.Index == n);

This isn't too bad, except that when there is no match at the starting offset then the entire input will be scanned unnecessarily, which is probably slower for most regexes than actually creating a substring prior to the match. (I didn't time it though).

Am I missing an obvious overload or setting that would restrict a match to the supplied offset only?

A: 

You can use \G at the start of your regex to ensure it matches at the beginning.

(^ doesn't work).

Testing in PowerShell:

PS> $r = [regex]"\Gabc"
PS> $r.IsMatch("01abc",2)
True
PS> $r.IsMatch("012abc",2)
False
Richard
Could you show an example please? I've shown one that doesn't work. ^ doesn't match at offsets.
romkyns
@romkyns: That shows I should test first :-(. But it appears `\G` does work... updating answer.
Richard
+3  A: 

Use \G:

var match = new Regex(@"\Gabc").Match("0123abc", n);

From the docs:

If you want to restrict a match so that it begins at a particular character position in the string and the regular expression engine does not scan the remainder of the string for a match, anchor the regular expression with a \G... This restricts the match so it must start exactly at startat.

Alan Moore