1

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:

  1. Find all text between char 0 and first (only) occurrence of \n\n, and fit that into format @"<p><b>%@</b><ul>",matchedString
  2. While there is still text, find all text between current char and next occurrence of \n (single) and fit into format @"<li>%@</li>"
  3. When there are no \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.

Answer implemented

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>"];
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126

2 Answers2

1

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
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Would I have to use `componentsSeparatedByString` twice, once to split it into two and again to split the second part into whatever number of bullet items are there? – tacos_tacos_tacos Mar 25 '12 at 12:53
  • @jshin47 Oh, I see, the only `\n\n` there is after the initial entry. You can split on `\n` then, do it once, and ignore the empty element at position 1 (it's the empty string between the first `\n` and the second `\n`). – Sergey Kalinichenko Mar 25 '12 at 12:59
  • Ok, that's what I went ahead and did. Very cool! I wasn't aware there was something like PHP `explode` available in Objc – tacos_tacos_tacos Mar 25 '12 at 13:02
0

you could use the method -[NSString componentsSeparatedByString:] to get what you want as an array of strings.

Nathan Day
  • 5,981
  • 2
  • 24
  • 40