2015-01-15 66 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