开发者

C# Regex format problem

I have the f开发者_StackOverflowollowing information in a config file:

Begin  
0  
0  
13  
44  
59  
047  
8784  

I'm reading that information and a ton of other information from the config file in as l.

My regex is:

string points = points = Regex.Match(l, @"BEGIN\r\n(\d+)$").Groups[1].Value;

it's returning ""

Where am I going wrong with this Regex?

Real solution remove the $:

string points = points = Regex.Match(l, @"POINTS\r\n(\d+)").Groups[1].Value;


You are assuming that your file ends after the first number. To match your first regex, the file would have to look like this:

(whatever we want)
BEGIN
0

The $ matches the end of your input. Also note the casing on "BEGIN" - if you do not explicitly pass RegexOptions.IgnoreCase, your regex will be case sensitive


The $ symbol denotes end of line, however you are performing a multi line regex. I suspect that is why removing the $ fixes the problem.

Consider passing in the third optional parameter for RegexOptions and specify MultiLine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜