简体   繁体   English

Regex.Match、startat 和 ^(字符串开头)

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

Does some knows why the output of this code:有人知道为什么这个代码的output:

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匹配 1 成功:错误

Match 2 sucess: True第 2 场比赛成功:真

? ?

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).预期的行为当然是“真”和“真”(否则我真的不知道“startat”参数应该有什么用处)。

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.这个想法是,这种正则表达式匹配(并且有很多)经常被调用(每秒几个 tousand),我们发现 substring 操作正在扼杀 memory 的性能。

Thanks for your help!谢谢你的帮助!

According to MSDN根据 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).如果要限制匹配,使其从字符串中的特定字符 position 开始,并且正则表达式引擎不扫描字符串的其余部分以进行匹配,请使用 \G 锚定正则表达式(在左侧从左到右的模式,或在右边的从右到左的模式)。 This restricts the match so it must start exactly at startat.这限制了匹配,因此它必须准确地从 startat 开始。

The regexp is matched with the entire string, you will need to use \G instead of ^正则表达式与整个字符串匹配,您需要使用 \G 而不是 ^

http://msdn.microsoft.com/en-us/library/3583dcyh.aspx 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. ^bar将仅匹配以“bar”开头的行,这是您用Substring人工完成的。 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.顺便说一句, Substring应该大多数正则表达式操作快得多。 I'd be surprised if that's what's killing your performance.如果这会扼杀你的表现,我会感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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