C#正则表达式麻烦(C# regular expression trouble)

编程入门 行业动态 更新时间:2024-10-25 18:28:50
C#正则表达式麻烦(C# regular expression trouble)

问题!

我有一个平面文件的以下输入(规则)(谈论数字输入):

输入可能是一个自然数(低于1000): 1, 10, 100, 999, ... 输入可能是用引号括起来的逗号分隔数字(1000以上): "1,000", "2,000", "3,000", "10,000", ...

我有以下正则表达式来验证输入:( (?:(\d+)|\x22([0-9]+(?:,[0-9]+)*)\x22) ,所以对于像这样的输入10我期待第一个匹配组10 ,这正是我得到的。 但是当我得到像"10,000"这样的输入时,我期望在第一个匹配组10,000 ,但它存储在第二个匹配组中。

string text1 = "\"" + "10,000" + "\""; string text2 = "50"; string pattern = @"(\d+)|\x22([0-9]+(?:,[0-9]+){0,})\x22"; Match match1 = Regex.Match(text1, pattern); Match match2 = Regex.Match(text2, pattern); if (match1.Success) { Console.WriteLine("Match#1 Group#1: " + match1.Groups[1].Value); Console.WriteLine("Match#1 Group#2: " + match1.Groups[2].Value); # Outputs # Match#1 Group#1: # Match#1 Group#2: 10,000 } if (match2.Success) { Console.WriteLine("Match#2 Group#1: " + match2.Groups[1].Value); Console.WriteLine("Match#2 Group#2: " + match2.Groups[2].Value); # Outputs # Match#2 Group#1: 50 # Match#2 Group#2: }

预期结果

两者都在相同的匹配组上产生,在这种情况下为1

有问题吗?

我究竟做错了什么? 我正在从正则表达式匹配中得到错误的分组。 另外,我正在使用filehelpers .NET来解析文件,有没有其他方法可以解决这个问题。 Actualy我正在尝试实现自定义转换器。

对象文件

[FieldConverter(typeof(OOR_Quantity))] public Int32 Quantity;

OOR_Quantity

internal class OOR_Quantity : ConverterBase { public override object StringToField(string from) { string pattern = @"(?:(\d+)|\x22([0-9]+(?:,[0-9]+)*)\x22)"; Regex regex = new Regex(pattern); if (regex.IsMatch(from)) { Match match = regex.Match(from); return int.Parse(match.Groups[1].Value); } throw new ... } }

Problem!

I Have the following input (rules) from a flat file (talking about numeric input):

Input might be a natural number (below 1000): 1, 10, 100, 999, ... Input might be a comma separated number surrounded by quotes (above 1000): "1,000", "2,000", "3,000", "10,000", ...

I Have the following regular expression to validate the input: (?:(\d+)|\x22([0-9]+(?:,[0-9]+)*)\x22), So for an input like 10 I'm expecting in the first matching group 10, which is exactly what I got. But when I got an input like "10,000" I'm expecting in the first matching group 10,000, but it is stored at the second matching group.

Example

string text1 = "\"" + "10,000" + "\""; string text2 = "50"; string pattern = @"(\d+)|\x22([0-9]+(?:,[0-9]+){0,})\x22"; Match match1 = Regex.Match(text1, pattern); Match match2 = Regex.Match(text2, pattern); if (match1.Success) { Console.WriteLine("Match#1 Group#1: " + match1.Groups[1].Value); Console.WriteLine("Match#1 Group#2: " + match1.Groups[2].Value); # Outputs # Match#1 Group#1: # Match#1 Group#2: 10,000 } if (match2.Success) { Console.WriteLine("Match#2 Group#1: " + match2.Groups[1].Value); Console.WriteLine("Match#2 Group#2: " + match2.Groups[2].Value); # Outputs # Match#2 Group#1: 50 # Match#2 Group#2: }

Expected Result

Both results on the same matching group, in this case 1

Questions?

What am I doing wrong? I'm just getting bad grouping from the regular expression matches. Also, I'm using filehelpers .NET to parse the file, is there any other way to resolve this problem. Actualy I'm trying to implement a custom converter.

Object File

[FieldConverter(typeof(OOR_Quantity))] public Int32 Quantity;

OOR_Quantity

internal class OOR_Quantity : ConverterBase { public override object StringToField(string from) { string pattern = @"(?:(\d+)|\x22([0-9]+(?:,[0-9]+)*)\x22)"; Regex regex = new Regex(pattern); if (regex.IsMatch(from)) { Match match = regex.Match(from); return int.Parse(match.Groups[1].Value); } throw new ... } }

最满意答案

组编号纯粹根据它们在正则表达式中的位置来分配 - 具体来说,是开括号的相对位置, ( 。在你的正则表达式中, (\d+)是第一组和([0-9]+(?:,[0-9]+)*)是第二个。

如果要使用相同的标识符引用它们,请使用命名组并为它们指定相同的名称:

@"(?:(?<NUMBER>\d+)|\x22(?<NUMBER>[0-9]+(?:,[0-9]+)*)\x22)"

现在,您可以将捕获的值检索为match.Groups["NUMBER"].Value 。

Group numbers are assigned purely on the basis of their positions in the regex--specifically, the relative position of the opening bracket, (. In your regex, (\d+) is the first group and ([0-9]+(?:,[0-9]+)*) is the second.

If you want to refer to them both with the same identifier, use named groups and give them both the same name:

@"(?:(?<NUMBER>\d+)|\x22(?<NUMBER>[0-9]+(?:,[0-9]+)*)\x22)"

Now you can retrieve the captured value as match.Groups["NUMBER"].Value.

更多推荐

本文发布于:2023-08-05 03:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:麻烦   正则表达式   regular   expression   trouble

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!