2015-01-15 39 views
0

我需要在字符串解析一点帮助...以下是他从FaceID结果字符串:汉王FaceID导致字符串解析

Return(result=\"success\" dev_id=\"6714113100001517\" total=\"24\"\r\n 
time=\"2014-06-16 17:56:44\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"0\" authority=\"0X55\" card_src=\"from_check\"\r\n 
time=\"2014-06-16 17:57:45\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"0\" authority=\"0X55\" card_src=\"from_check\"\r\n 
time=\"2014-06-16 17:57:58\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"2\" authority=\"0X55\" card_src=\"from_check\"\r\n 
time=\"2014-06-16 17:58:02\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"1\" authority=\"0X55\" card_src=\"from_check\"\r\n 
time=\"2014-06-16 17:58:04\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"0\" authority=\"0X55\" card_src=\"from_check\"\r\n 
time=\"2014-06-16 17:58:19\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"2\" authority=\"0X55\" card_src=\"from_check\"\r\n 
time=\"2014-11-29 13:23:36\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"0\" authority=\"0X55\" card_src=\"from_check\"\r\n 
time=\"2014-11-29 13:23:46\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"2\" authority=\"0X55\" card_src=\"from_check\"\r\n 
time=\"2014-11-29 13:23:49\" id=\"399\" name=\"\" workcode=\"0\" 
status=\"0\" authority=\"0X55\" card_src=\"from_check\"\r\n) 

我知道我如何通过字符串和匹配令牌环,但不知道是否有可能是这种类型的字符串的任何正则表达式?或者,如果很容易将其转换为XML或JSON?如果是这样,那么性能会更好?

我想要时间,ID,名称,工作代码,状态,权限,card_src的单独值 - 例如对象的列表或集合。

回答

0

要分开,你可以使用的值:

\b(time|id|name|workcode|status|authority|card_src)=\\"(.*?)\\" 

DEMO


在捕获组1时获得的名称。
在捕获组2中,您将获得该值。

+0

谢谢,让我尝试..如果它工作 – nufshm

0

肯定的 - 你可以做一个正则表达式,如:

time=\\"([^\\]+)\\" id=\\"([^\\]+)\\" name=\\"([^\\]+)\\" workcode=\\"([^\\]+)\\" status=\\"([^\\]+)\\" authority=\\"([^\\]+)\\" card_src=\\"([^\\]+)\\" 

然后遍历组每场比赛中看到匹配的值(这将是语言特定代码)

在正则表达式以上
\\表示\(反斜杠必须被转义)
()表示捕获组(因此所捕获的值可以在结果中引用
[^\\]+指一类是不是字符的一个\重复一次或更多次

这是C#代码通过匹配,以提取所拍摄的值循环的示例:

var input = "your input string... cant be bothered to repeat it here!"; 
var pattern= @"time=\\""([^\\]+)\\"" id=\\""([^\\]+)\\"" name=\\""([^\\]+)\\"" workcode=\\""([^\\]+)\\"" status=\\""([^\\]+)\\"" authority=\\""([^\\]+)\\"" card_src=\\""([^\\]+)\\"""; 
var matches = Regex.Matches(input, pattern); 
foreach (var match in matches) 
{ 
    Console.WriteLine("time is: {0}", match.Groups[1]); 
    Console.WriteLine("id is: {0}", match.Groups[2]); 
    Console.WriteLine("name is: {0}", match.Groups[3]); 
    // and so on... 
} 
+0

谢谢,让我试试..如果它的工作原理 – nufshm