here xml received via webservice..
<?xml version="1.0" encoding="utf-8"?> <quotes xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns="http://swanandmokashi.com"> <quoteoftheday>hit user continue.</quoteoftheday> <author>fuzzel fish administration page</author> </quotes> and im trying parse in connectiondidfinishloading: ...
{ webdata_str=[[nsstring alloc]initwithdata:webdata encoding:nsutf8stringencoding]; nsstring *pattern = @"<quoteoftheday>(.*)</quoteoftheday><author>(.*)</author>"; nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:pattern options:nsregularexpressioncaseinsensitive error:nil]; __block nsstring *quotestring,*author= nil; [regex enumeratematchesinstring:webdata_str options:0 range:nsmakerange(0, [webdata_str length]) usingblock:^(nstextcheckingresult *match, nsmatchingflags flags, bool *stop) { // not executing... if (0 < [match numberofranges]) { nsrange range = [match rangeatindex:1]; quotestring = [webdata_str substringwithrange:range]; nsrange range1 = [match rangeatindex:2]; author = [webdata_str substringwithrange:range1]; } }]; final=[nsstring stringwithformat:@"\n%@\n%@",quotestring,author]; nslog(@"final--- %@",final); } what's wrong in this? , execution flow not going inside block..
a better solution use xml parser (e.g. nsxmlparser) instead of regular expressions.
but answer concrete problem: regex not match whitespace (newline , spaces) between </quoteoftheday> , <author>. if change pattern to
nsstring *pattern = @"<quoteoftheday>(.*)</quoteoftheday>\\s*<author>(.*)</author>"; you expected output.
Comments
Post a Comment