I have this regular expression:
(?'box_id'\d{1,19})","box_name":"(?'box_name'[\w\d\.\s]{1,19})
This works well, except when the box name contains spaces. For example, when executing it on my box it returns mybox, without the space.
How can I make it include spaces in the box_name group?
Code:
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;
}
}
Input:
{“objects”:[{“object_id”:”135248″,”file_name”:”some space 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,Hon(olulu,Hawaii.jpg”,”video_status”:”0″,”thumbnail_status”:”1″},{“object_id”:”135280″,”file_name”:”test with spaces.jpg”,”video_status”:”0″,”thumbnail_status”:”1″}],”status”:”ok”}
It appears to me that your data is consistently double quote delimited, no? That fact should be the basis of the regex:
As far as missing spaces, this token, (?’box_name'[\w\d.\s]{1,19}) , cannot match ‘mybox’ on a string containing ‘my box’, so that issue must be downstream.
Typos and style: you have the literal ‘box_name’ but the tokens are ‘file_name’. Also, why in the world would you switch to using single quotes as the named group delimiter when <> brackets, the default, are MORE readable (since quotes are in the regex!)