捕获计数始终为零
我有一个问题。我使用以下正则表达式:
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}
- 我需要捕获
2008
、5528.35
、2009
、8653.89
、2010
、4290.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:您可以通过匹配所有内容直到下一个逗号来简化:
[^,]*
。这是与您的源数据相匹配的完整代码片段:这将打印:
Edit: You could simplify by matching everything until the next comma:
[^,]*
. Here's a full code snippet to match your source data:This prints:
我认为问题是一个逗号:
它应该是可选的(或省略?):
I think the problem is a comma:
which should be optional (or omitted?):
解释一下 MRAB 所说的话:
To expound on what MRAB said: