Regular expressions / pulling string from inside a string

Discussion in 'iOS Programming' started by dusker, Jul 21, 2009.

  1. dusker macrumors member

    Joined:
    May 31, 2009
    #1
    Hi Guys!
    I'm working on an app of mine, but i came across a problem i find pretty hard to bite. Inside a NSString i'll have a pretty long html code, and i'd like to pull out of that string, substrings that are contained inside certain tags. So for example i'd like to get what's inside <font><b> and </b></font> tags.
    There'll be couple of lines containing these tags so i'll need this solution to run inside a loop.

    Any help will be greatly appreciated.

    greetings
    peter
     
  2. dusker thread starter macrumors member

    Joined:
    May 31, 2009
    #3
    @jnic thanks for the clue! Would you (or someone else) care to post a short snippet of what methods to use? Say we got the following:

    Code:
    NSString *str = @"<html><body><font><b>hello</b></font> <font><b>world</b></font></body></html>";
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:str];
    
    how to get the parser to return what's enclosed inside <font><b> and </b></font>

    thanks in advance
     
  3. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #4
    Check this example:

    https://developer.apple.com/iphone/...ts.html#//apple_ref/doc/uid/20002265-BCIJFGJI

    in short you would probably want to set up 2 BOOLs like inFont and inB so you know when you are within <font><b>, capture all data until the tags are closed and then clear the BOOLs again
     
  4. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #5
    Depending on your needs, NSXMLParser may be overkill; you might be able to accomplish what you want using regular expressions - try the RegexKitLite framework.
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    use Javascript

    Javascript supports regex, and there are several xml/html parsing libraries available for Javascript. You can run the JS code inside a hidden UIWebview. Calling a JS parser from Obj-C might be a lot easier than rolling the equivalent parser in Obj-C.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Well, I'm gonna suggest looking into NSScanner if the structure of what you're pulling from is not overly complex.
     
  7. dusker thread starter macrumors member

    Joined:
    May 31, 2009
    #8
    thanks for a tip, could you recommend any paticular js html parser?

    I think the html i need to parse is too complex for nsscanner:(

    Would anyone care to post some snippet of regex expression that would pull data from inside <font><b> tag?
     
  8. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #9

    /<font><b>(.*?)<\/b><\/font>/ism
     

Share This Page