Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to compile a NSRegularExpression to match multiple strings?

Tags:

ios

ICU and Java's regular expression support (and probably other platforms) separate compilation of a regular expression from matching it to a specific string. This improves performance when a common regex pattern is matched with multiple strings, since it only needs to be compiled once.

Is there any way to do this with NSRegularExpression? Its design appears to combine these two steps, if I'm reading the documentation correctly.

like image 729
tball Avatar asked Dec 01 '25 15:12

tball


1 Answers

They are two steps. First, you create a regular expression:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<h1>(.*?)</h1>"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];

And then, second, you use it (obviously use whatever method you want):

[regex enumerateMatchesInString:htmlString
                        options:0
                          range:NSMakeRange(0, [htmlString length])
                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                         // do whatever you want
                 }];

Am I misunderstanding the question?

like image 124
Rob Avatar answered Dec 04 '25 07:12

Rob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!