简体   繁体   中英

Regex.Match, startat and ^ (start of string)

Does some knows why the output of this code:

Regex re = new Regex("^bar", RegexOptions.Compiled);
string fooBarString = @"foo bar";

Match match1 = re.Match(fooBarString, 4);
Console.WriteLine(String.Format("Match 1 sucess: {0}", match1.Success));

Match match2 = re.Match(fooBarString.Substring(4));
Console.WriteLine(String.Format("Match 2 sucess: {0}", match2.Success));

is:

Match 1 sucess: False

Match 2 sucess: True

?

Expected behaviour is of course "True" and "True" (or else I really don't know what the "startat" parameter is supposed to be useful for).

The idea is that this regex matching (and there are lots of them) is called very often (several tousand per second) and we discovered that the substring operations are killing memory performance.

Thanks for your help!

According to MSDN

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 (at the left for a left-to-right pattern, or at the right for a right-to-left pattern). This restricts the match so it must start exactly at startat.

The regexp is matched with the entire string, you will need to use \G instead of ^

http://msdn.microsoft.com/en-us/library/3583dcyh.aspx

Sounds like you're right - you're confused about the meaning of ^ . ^ means the very beginning of the line you're working with. ^bar will only match lines that begin with "bar", which you've artificially done with Substring there. We might be able to help you if you explain what you're trying to do with it.

Incidentally, Substring should be significantly faster than most regex operations. I'd be surprised if that's what's killing your performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM