捕获计数始终为零

发布于 2024-11-10 10:25:23 字数 1710 浏览 5 评论 0原文

我有一个问题。我使用以下正则表达式:


Pattern =
  (?'name'\w+(?:\w|\s)*), \s*
  (?'category'\w+(?:\w|\s)*), \s*
  (?:
      \{ \s*
          [yY]: (?'year'\d+), \s*
          [vV]: (?'volume'(?:([1-9][0-9]*\.?[0-9]*)|(\.[0-9]+))+), \s*
      \} \s*
      ,? \s*
  )*

带有 IgnorePatternWhitespaces 选项。 在我调试它之前,我的应用程序中的一切看起来都很好。遇到问题。


var Year = default(UInt32);
// ...
if((Match = Regex.Match(Line, Pattern, Options)).Success)
{
    // Getting Product header information
    Name = Match.Groups["name"].Value;

    // Gathering Product statistics
    for(var ix = default(Int32); ix < Match.Groups["year"].Captures.Count; ix++)
    {
       // never get here
       Year = UInt32.Parse(Match.Groups["year"].Captures[ix].Value, NumberType, Culture);
    }
}

所以在上面的代码中..在我的例子中,匹配总是成功的。我获得了 Name 的正确值,但是当轮到 for 循环程序流时,它就通过了。我调试了 Match.Groups["year"] 中没有 Captures。所以这是合乎逻辑的行为。但对我来说并不明显我错在哪里。帮助!!

我之前有一篇相关文章提取大括号内的数值

谢谢!

编辑。 输入样本

Sherwood, reciever, {y:2008,V:5528.35}, {y:2009,V:8653.89}, {y:2010, V:4290.51}
  • 我需要捕获 20085528.3520098653.8920104290.51 值并将它们作为命名组进行操作。

2D 编辑

我尝试使用 ExplicitCapture 选项和以下表达式:

(?<name>\w+(w\| )*), (?<category>\w+(w\| )*), (\{[yY]:(?<year>\d+), *[vV]:(?<volume>(([1-9][0-9]*\.?[0-9]*)|(\.[0-9]+))+)\}(, )?)+

但这没有帮助。

I've got a problem. I use following regular expression:


Pattern =
  (?'name'\w+(?:\w|\s)*), \s*
  (?'category'\w+(?:\w|\s)*), \s*
  (?:
      \{ \s*
          [yY]: (?'year'\d+), \s*
          [vV]: (?'volume'(?:([1-9][0-9]*\.?[0-9]*)|(\.[0-9]+))+), \s*
      \} \s*
      ,? \s*
  )*

with IgnorePatternWhitespaces option.
Everything seemed fine in my application until I debugged it & encountered a problem.


var Year = default(UInt32);
// ...
if((Match = Regex.Match(Line, Pattern, Options)).Success)
{
    // Getting Product header information
    Name = Match.Groups["name"].Value;

    // Gathering Product statistics
    for(var ix = default(Int32); ix < Match.Groups["year"].Captures.Count; ix++)
    {
       // never get here
       Year = UInt32.Parse(Match.Groups["year"].Captures[ix].Value, NumberType, Culture);
    }
}

So in the code above.. In my case Match is always successful. I get proper value for Name but when turn comes to for loop program flow just passes it by. I debugged there's no Captures in Match.Groups["year"]. So it is logical behavior. But not obvious to me where I'm wrong. Help!!

There is a previous connected post Extract number values enclosed inside curly brackets I made.

Thanks!

EDIT. Input Samples

Sherwood, reciever, {y:2008,V:5528.35}, {y:2009,V:8653.89}, {y:2010, V:4290.51}
  • I need to capture 2008, 5528.35, 2009, 8653.89, 2010, 4290.51 values and operate with them as named groups.

2D EDIT

I tried using ExplicitCapture Option and following expression:

(?<name>\w+(w\| )*), (?<category>\w+(w\| )*), (\{[yY]:(?<year>\d+), *[vV]:(?<volume>(([1-9][0-9]*\.?[0-9]*)|(\.[0-9]+))+)\}(, )?)+

But that didn't help.

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

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

发布评论

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

评论(3

病毒体 2024-11-17 10:25:23

编辑:您可以通过匹配所有内容直到下一个逗号来简化:[^,]*。这是与您的源数据相匹配的完整代码片段:

var testRegex = new Regex(@"
    (?'name'[^,]*),\s*
    (?'category'[^,]*),\s*
    ({y:(?'year'[^,]*),\s*
    V:(?'volume'[^,]*),?\s*)*",
    RegexOptions.IgnorePatternWhitespace);
var testMatches = testRegex.Matches(
    "Sherwood, reciev, {y:2008,V:5528.35}, {y:2009,V:8653.89}, {y:2010, V:4290.51}");
foreach (Match testMatch in testMatches)
{
    Console.WriteLine("Name = {0}", testMatch.Groups["name"].Value);
    foreach (var capture in testMatch.Groups["year"].Captures)
        Console.WriteLine("    Year = {0}", capture);
}

这将打印:

Name = Sherwood
    Year = 2008
    Year = 2009
    Year = 2010

Edit: You could simplify by matching everything until the next comma: [^,]*. Here's a full code snippet to match your source data:

var testRegex = new Regex(@"
    (?'name'[^,]*),\s*
    (?'category'[^,]*),\s*
    ({y:(?'year'[^,]*),\s*
    V:(?'volume'[^,]*),?\s*)*",
    RegexOptions.IgnorePatternWhitespace);
var testMatches = testRegex.Matches(
    "Sherwood, reciev, {y:2008,V:5528.35}, {y:2009,V:8653.89}, {y:2010, V:4290.51}");
foreach (Match testMatch in testMatches)
{
    Console.WriteLine("Name = {0}", testMatch.Groups["name"].Value);
    foreach (var capture in testMatch.Groups["year"].Captures)
        Console.WriteLine("    Year = {0}", capture);
}

This prints:

Name = Sherwood
    Year = 2008
    Year = 2009
    Year = 2010
芸娘子的小脾气 2024-11-17 10:25:23

我认为问题是一个逗号:

, \s* \}

它应该是可选的(或省略?):

,? \s* \}

I think the problem is a comma:

, \s* \}

which should be optional (or omitted?):

,? \s* \}
吖咩 2024-11-17 10:25:23

解释一下 MRAB 所说的话:

(?'name'
    \w+
    (?:
       \w|\s
    )*
),
\s* 
(?'category'
     \w+
     (?:
         \w|\s
     )*
),
\s* 
(?:
      \{ 
          \s*
          [yY]:
          (?'year'
               \d+
          ),
          \s*
          [vV]:
          (?'volume'
               (?:
                   (     # Why do you need capturing parenth's here ?
                     [1-9][0-9]*
                     \.?
                     [0-9]*
                   )
                 |
                   (
                     \.[0-9]+
                   )
               )+
          ),        # I'm just guessing this comma doesent match input samples
          \s*
      \}
      \s*
      ,?
      \s*
)*


Sherwood, reciever, {y:2008,V:5528.35}, {y:2009,V:8653.89}, {y:2010, V:4290.51}

To expound on what MRAB said:

(?'name'
    \w+
    (?:
       \w|\s
    )*
),
\s* 
(?'category'
     \w+
     (?:
         \w|\s
     )*
),
\s* 
(?:
      \{ 
          \s*
          [yY]:
          (?'year'
               \d+
          ),
          \s*
          [vV]:
          (?'volume'
               (?:
                   (     # Why do you need capturing parenth's here ?
                     [1-9][0-9]*
                     \.?
                     [0-9]*
                   )
                 |
                   (
                     \.[0-9]+
                   )
               )+
          ),        # I'm just guessing this comma doesent match input samples
          \s*
      \}
      \s*
      ,?
      \s*
)*


Sherwood, reciever, {y:2008,V:5528.35}, {y:2009,V:8653.89}, {y:2010, V:4290.51}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文