我有這樣的正則表達式:正則表達式匹配不包括空間
(?'box_id'\d{1,19})","box_name":"(?'box_name'[\w\d\.\s]{1,19})
這種運作良好,當文件夾名稱包含空格除外。例如,在my box
上執行時,它將返回mybox
,但不包含空格。
如何讓它包含box_name
組中的空格?
代碼:
Regex reg = new Regex(@"""object_id"":""(?<object_id>\d{1,19})"",""file_name"":""(?<file_name>[\w.]+(?:\s[\w.]+)*)""");
MatchCollection matches = reg.Matches(result);
if (matches == null) throw new Exception("There was an error while parsing data.");
if (matches.Count > 0)
{
FileArchive.FilesDataTable filesdataTable = new FileArchive.FilesDataTable();
foreach (Match match in matches)
{
FileArchive.FilesRow row = filesdataTable.NewFilesRow();
row.ID = match.Groups["object_id"].Value;
row.Name = match.Groups["file_name"].Value;
}
}
輸入:
{ 「對象」:[{ 「的object_id」: 「135248」, 「FILE_NAME」:「一些空間here.jpg 「,」video_status「:」0「,」thumbnail_status「:」1「},{」object_id「:」135257「,」file_name「:」jup 13.jpg「,」video_status「:」0「」thumbnail_status「 :「1」},{「object_id」:「135260」,「file_name」:「my pic.jpg」,「video_status」:「0」,「thumbnail_status」:「1」},{「object_id」:「135262 「 」FILE_NAME「:」 EveningWav)ES,漢(olulu,H awaii.jpg「,」video_status「:」0「,」thumbnail_status「:」1「},{」object_id「:」135280「,」file_name「:」test with spaces.jpg「,」video_status「:」 ,「thumbnail_status」:「1」}],「status」:「ok」}
你可以給我們的那種你正在試圖解析字符串的例子嗎? – 2011-12-15 19:19:11
類似於:{「objects」:[{「object_id」:「135260」,「file_name」:「my pic.jpg」,「video_status」:「0」,「thumbnail_status」:「1」},{「object_id 「:」135261「,」file_name「:」New Text Document.txt「,」video_status「:」0「,」thumbnail_status「:」0「}],」status「:」ok「} – 2011-12-15 19:21:33
這裏有兩個文件名,我的pic.jpg和新文本Document.txt – 2011-12-15 19:23:35