Using NSRegularExpression to extract URLs on the iPhone

后端 未结 5 622
耶瑟儿~
耶瑟儿~ 2020-11-30 09:16

I\'m using the following code on my iPhone app, taken from here to extract all URLs from striped .html code.

I\'m only being able to extract the first URL, but I nee

5条回答
  •  被撕碎了的回忆
    2020-11-30 09:57

    I found myself so nauseated by the complexity of this simple operation ("match ALL the substrings") that I made a little library I am humbly calling Unsuck which adds some sanity to NSRegularExpression in the form of from and allMatches methods. Here's how you'd use them:

    NSRegularExpression *re = [NSRegularExpression from: @"(?i)\\b(https?://.*)\\b"]; // or whatever your favorite regex is; Hossam's seems pretty good
    NSArray *matches = [re allMatches:httpLine];
    

    Please check out the unsuck source code on github and tell me all the things I did wrong :-)

    Note that (?i) makes it case insensitive so you don't need to specify NSRegularExpressionCaseInsensitive.

提交回复
热议问题