使用普通防爆pression用C验证#.NET应用程序了 [英] Validation using Regular Expression in C#.net applicaiton
问题描述
我们要验证输入.dat文件记录提供deliminators。
We want to validate input .DAT file records for provided deliminators.
在我们的.NET应用程序中,我们可以解析输入文件按规定deliminator,所有deliminator是正确的, 例如:测试,数据,CaseInforation
In our .net application we can parse input file as per provided deliminator where all deliminator are proper , ex : "Test","data","CaseInforation"
上面记录/从文件中行将解析成功,现在我们有问题,行/记录的格式如下:
above record/row from file will parse successfully,now we have problem with row/record formatted as below:
测试,数据CaseInforation (ü可以看有没有文本限定符包围数据值放大器,这是因为它在从文件解析的数据产生的问题)。
"Test",data,"CaseInforation" (u can see there is no text qualifier surrounded with 'data' value & because of this it creates problem while parsing data from file).
所以我们决定使用常规的前pression以找到那些没有周围有TEXTQUALIFIER问题的价值。
So we decided to use regular expression to find problematic value which are not surrounded with TextQualifier.
要解决这个问题,我们已经创建了下面的正则表达式来找到问题的价值, \ X2C([^ \ X22] *?[^ \ X22])\ X2C
To Resolve this problem, we have created below RegEx to find problematic value, \x2C([^\x22].*?[^\x22])\x2C
使用上述正则EX pression,它在记录中第一个与放大器之间;最后领域。
using above regular expression, it works in records in between first & last fields.
测试,数据CaseInforation - >常规EX pression解析这个记录成功和放大器;提供的数据作为输出域有问题。
"Test",data,"CaseInforation" -> Regular expression parse this records successfully & providing data as output field having problem.
测试,数据,CaseInforation - >此记录定期EX pression不匹配最后一个值
"Test","data",CaseInforation -> for this record regular expression does not match last value.
任何一个可以帮助我们正确的正前pression这将匹配第一个或最后一个值。
can any one help us to correct Regular Expression which would match first or last value.
感谢。
推荐答案
^(:( ?:((?:?| [^])+)|([^ ,] *))(?:$ |,))+ $
将匹配的整条生产线,那么你可以使用match.Groups [1] .Captures让你的数据了(不带引号) 。另外,我让我的名字是在引号
是一个有效的字符串。
^(?:(?:"((?:""|[^"])+)"|([^,]*))(?:$|,))+$
Will match the whole line, then you can use match.Groups[1].Captures to get your data out (without the quotes). Also, I let "My name is ""in quotes"""
be a valid string.
string mystring = "\"Test\",\"data\",\"CaseInforation\"";
MatchCollection matches = Regex.Matches(mystring, "^(?:(?:\"((?:\"\"|[^\"])+)\"|([^,]*))(?:$|,))+$");
match[0].Value = "Test","data","CaseInforation"
match[0].Groups[0].Value => "Test","data","CaseInforation"
match[0].Groups[0].Captures[0].Value => "Test","data","CaseInforation"
match[0].Groups[1].Value => CaseInforation
match[0].Groups[1].Captures[0].Value => Test
match[0].Groups[1].Captures[1].Value => data
match[0].Groups[1].Captures[2].Value => CaseInforation
这篇关于使用普通防爆pression用C验证#.NET应用程序了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!