My app accesses a poor web service that returns output like this:
Title\n\nInfo1\nInfo2\nInfo3\n ...
To clarify, the text is not literally ...
, and the info is not literally numbered, I just wanted to show that they are distinct.
I want to display it in a UIWebView
like this:
<p><b>Title</b><ul><li>Info1</li><li>Info2</li><li>Info3</li></ul></p>
How can I accomplish this using either NSRegularExpression
or NSScanner
? I am confused about NSRegularExpression
both because I do not know the correct expression to use, and I'm not sure how I access the results to place it in a neat formatted string.
Naively, the logic would be something like this:
\n\n
, and fit that into format @"<p><b>%@</b><ul>",matchedString
\n
(single) and fit into format @"<li>%@</li>"
\n
left, append @"</ul></p>"
and call it a day.Please advise on how I can implement this logic, or alternatively encode it in a regular expression.
I just wanted to post my makeshift implementation of the answer in case anybody in the future get stuck:
NSArray *parts = [self.dogFoodBenefits componentsSeparatedByString:@"\n"];
NSString *benefitsString = [NSString stringWithFormat:@"<h4>Benefits</h4><b>%@</b><p><ul>",[parts objectAtIndex:0]];
for (int i = 2; i < [parts count]; i++) {
if ([[parts objectAtIndex:i] length] > 1)
benefitsString = [benefitsString stringByAppendingFormat:@"<li>%@</li>",[parts objectAtIndex:i]];
}
benefitsString = [benefitsString stringByAppendingString:@"</ul></p>"];
I think you'd have easier time simply splitting the string at \n\n
using componentsSeparatedByString:
method:
NSArray *parts = [webTextStr componentsSeparatedByString:@"\n\n"];
// Use the initial or the only element inside <b></b> tag
// Put the remaining elements, if any, in the <li></li> tags