2

I've been working on my own simple Wikipedia parser in C#. So far I'm not getting very far because of this problem.

I extract the string {e|24} but it could contain any number. All I want to do is simply extract the number from this string.

This is the code I am using currently:

Match num = Regex.Match(exp.Value, "[0-9]*");
Console.WriteLine(num.Value);

However num.Value is blank.

Can someone please explain why this is not working and how I can fix it?

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
Benny33
  • 475
  • 1
  • 6
  • 12
  • This is trivial to test in a tool. I use http://www.radsoftware.com.au/regexdesigner/ which will give you exact results for .NET. – Chuck D Jan 09 '13 at 23:56
  • To further explain why this didn't work, [0-9]* actually matches your string in 6 places: `><{e|24}`, `{><|24}`, `{e|>24<}`, `{e|24><}`, and `{e|24}><`. So it did find the part you wanted, but it also matched 5 empty strings, and Match() only returns the first match. – JLRishe Jan 10 '13 at 00:51

2 Answers2

6

You would want to use [0-9]+ to ensure at least one number. [0-9]* allows it to be matched 0 times or more, thus getting blanks

CorrugatedAir
  • 809
  • 1
  • 6
  • 16
1

My suggestion, make the regexp: \d+

Works. Simpler. Shorter, uses no groups or ranges.

hjc1710
  • 576
  • 4
  • 17