I need to parse string that looks like in example below:
Regex TitleRegex = new Regex(@"[A-Z].* - ([0-9].*) [A-Z]");
var match = TitleRegex.Match("Chapter - 1 The Brown Fox");
Console.WriteLine(match.Groups[1].Value);
What I want is to extract a number. The problem is that output is 1 The Brown instead of simply 1.
I do not understand why letters are also included to the numeric ([0-9]) pattern.
Any suggestions?
You are capturing the
.which generally is match all except new lines. I put the{1,2}quantifier there, meaning it will match 0-99. Change that to suit your requirements (or you could just leave it as 0 or more*).Could you also use
\dinstead of[0-9]. Shorthand is generally a good thing 🙂