提取大括号内的数值

发布于 2024-11-09 23:14:55 字数 688 浏览 0 评论 0原文

我想从给定文件中提取一些字符串数据。文件的结构如下:


name, catg, {y:2006, v:1000, c:100, vt:1}, {y:2007, v:1000, c:100, vt:1},。 {..}..


我想提取下一个值:

  • 名称;
  • 猫;
  • yvcvt 标签后的数字;

我使用了下一个正则表达式:

  • @"(?\w+), (?\w+)" 来提取前两项;
  • @"(?:\{y:(?\d+), +v:(?\d+), +c:(?\d+), +vt :(?\d+)\}, ?)+" 用于提取大括号中的其他值。

我将这两者连接起来并在正则表达式测试器中进行了测试。但正如预期的那样,我只得到一组提取的数字。我需要另一部分的结果({y:2007, v:1000, c:100, vt:1})。此外,可以有两个以上的部分。

如何修复我的正则表达式?然后我如何从相应的部分收集所有数字集。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2024-11-16 23:14:55

这是固定的正则表达式(您需要指定 IgnorePatternWhitespace 选项):

(?'name'\w+), \s*
(?'category'\w+), \s*
(?:
    \{ \s*
        y: (?'y'\d+), \s*
        v: (?'v'\d+), \s*
        c: (?'c'\d+), \s*
        vt: (?'vt'\d+)
    \} \s*
    ,? \s*
)*

这是用法:

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文