iOS Regular Expression to find multiple words

isthisonetaken

macrumors regular
Original poster
Jun 29, 2006
123
0
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!
 

xStep

macrumors 68020
Jan 28, 2003
2,013
99
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.

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

With out knowing more detail this is all I could come up with.
 
Comment

chown33

Moderator
Staff member
Aug 9, 2009
8,670
4,941
vertical
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.
 
Comment

isthisonetaken

macrumors regular
Original poster
Jun 29, 2006
123
0
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.
 
Comment

Similar threads

Register on MacRumors! This sidebar will go away, and you'll see fewer ads.