问题!
我有一个平面文件的以下输入(规则)(谈论数字输入):
输入可能是一个自然数(低于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.
更多推荐
发布评论