问题
I want to extract some string data from a given file. File got structure such as:
name, catg, {y:2006, v:1000, c:100, vt:1}, {y:2007, v:1000, c:100, vt:1},.. {..}..
.
I want to extract next values:
- name;
- catg;
- numbers after
y
,v
,c
,vt
labels;
I used the next regexes:
@"(?<name>\w+), (?<cat>\w+)"
for extraction of the first two items;@"(?:\{y:(?<y>\d+), +v:(?<v>\d+), +c:(?<c>\d+), +vt:(?<vt>\d+)\}, ?)+"
for extraction of other values enclosed in curly brackets.
I concatenated those two and made a test in regex tester. But as expected I get only one set of extracted numbers. And I need result from the other part ({y:2007, v:1000, c:100, vt:1}
). Moreover there could be more than two parts.
How do I fix my regex? And then how do I collect all number sets from corresponding parts.
回答1:
Here's fixed regex (you need to specify IgnorePatternWhitespace option):
(?'name'\w+), \s*
(?'category'\w+), \s*
(?:
\{ \s*
y: (?'y'\d+), \s*
v: (?'v'\d+), \s*
c: (?'c'\d+), \s*
vt: (?'vt'\d+)
\} \s*
,? \s*
)*
And here's usage:
String input = @"name, catg, {y:2006, v:1000, c:100, vt:1}, {y:2007, v:1000, c:100, vt:1}";
String pattern =
@"(?'name'\w+), \s*
(?'category'\w+), \s*
(?:
\{ \s*
y: (?'y'\d+), \s*
v: (?'v'\d+), \s*
c: (?'c'\d+), \s*
vt: (?'vt'\d+)
\} \s*
,? \s*
)* ";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline;
Match match = Regex.Match(input, pattern, options);
if (match.Success)
{
String name = match.Groups["name"].Value;
String category = match.Groups["category"].Value;
Console.WriteLine("name = {0}, category = {1}", name, category);
for (Int32 i = 0; i < match.Groups["y"].Captures.Count; ++i)
{
Int32 y = Int32.Parse(match.Groups["y"].Captures[i].Value);
Int32 v = Int32.Parse(match.Groups["v"].Captures[i].Value);
Int32 c = Int32.Parse(match.Groups["c"].Captures[i].Value);
Int32 vt = Int32.Parse(match.Groups["vt"].Captures[i].Value);
Console.WriteLine("y = {0}, v = {1}, c = {2}, vt = {3}", y, v, c, vt);
}
}
来源:https://stackoverflow.com/questions/6163017/extract-number-values-enclosed-inside-curly-brackets