Regular Expression to find multiple words

Discussion in 'iOS Programming' started by isthisonetaken, May 26, 2012.

  1. isthisonetaken macrumors regular

    Jun 29, 2006
    Hey Guys,

    I'm searching through some text stored in an NSString object for a specific line of text like this: > TO Some Place Somewhere<. I have my code all set up so that all I need to find that is a regular expression pattern. I search for other pieces of info using patterns that start with >(my pattern) and all work. My problem is that the text I'm looking for can have different number of words each time, but I know it will always be between >< and starts with TO.

    Anyone that's good with regular expressions have any suggestions for me? I'm new with regular expressions can can't seem to make sense of what I'm finding on Google.

    Thanks for the help!
  2. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    I'm assuming you're using NSRegularExpression which I don't know much about. I did create the following pattern that maybe what you are looking for. It looks for multiple words within the knows outer characters you mention. It also accounts for non word characters such as a comma between words. An assumption is that before each word, there is a space.

    @"> TO( \\w*\\W?)+<"

    With out knowing more detail this is all I could come up with.
  3. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    You could set up a simple two-step search.
    1. Find the literal text "> TO". Note its position.
    2. After that position, find the literal text "<". Note that position.

    The result then lies between the two positions in the text.

    If that doesn't fit your code structure, write a method that encapsulates the two-step search, and the extraction and return of the result substring.
  4. isthisonetaken thread starter macrumors regular

    Jun 29, 2006
    I wound up using this to get the data I needed:

    <td align=\"left\" colspan=\"1\">(.*)<\\/td>

    It's pulling from an HTML file and I had to add in the align="left" colspan="1" otherwise it added that to the string. All is good now.

Share This Page