ios - Obj-c Seperate a string by double quote using regular expression -
i learnt regular expression today , have nsstring:
{"mon":["operation system;monday 1,2{9-16};professora;xx-229","database;monday3,4{1-16};professorb;xx-130","human computer interaction;monday5,6{1-8};professorc;xx-130;;c programming;monday5,6{9-16};professorc;xx-336",null,null,null]} i want separate , put nsarray (after separation):
mon operation system;monday 1,2{9-16};professora;xx-229 database;monday3,4{1-16};professorb;xx-130 human computer interaction;monday5,6{1-8};professorc;xx-130;;c programming;monday5,6{9-16};professorc;xx-336 and show separately tableviewcells, , regular expression this:
[^\\"] it job, not enough, because made separate single letters instead of string want. got idea?
you need match text in between quotes (edit: will capture escaped quotes, test includes \\\"monday):
nsstring *pattern = @"\"((?:[^\"\\\\]|\\\\.)*)\""; here sample code can progress:
nserror *error = nil; nsstring *pattern = @"\"((?:[^\"\\\\]|\\\\.)*)\""; nsstring *string = @"{\"mon\":[\"operation system;\\\"monday 1,2{9-16};professora;xx-229\",\"database;monday3,4{1-16};professorb;xx-130\",\"human computer interaction;monday5,6{1-8};professorc;xx-130;;c programming;monday5,6{9-16};professorc;xx-336\",null,null,null]}"; nsrange range = nsmakerange(0, string.length); nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:pattern options:0 error:&error]; nsarray *matches = [regex matchesinstring:string options:0 range:range]; (nstextcheckingresult* match in matches) { nsrange group1 = [match rangeatindex:1]; nslog(@"group1: %@", [string substringwithrange:group1]); } demo program output:
2015-05-14 08:45:37.906 main[33] group1: mon 2015-05-14 08:45:37.908 main[33] group1: operation system;\"monday 1,2{9-16};professora;xx-229 2015-05-14 08:45:37.908 main[33] group1: database;monday3,4{1-16};professorb;xx-130 2015-05-14 08:45:37.908 main[33] group1: human computer interaction;monday5,6{1-8};professorc;xx-130;;c programming;monday5,6{9-16};professorc;xx-336
Comments
Post a Comment