Extract text using NSRegularExpression

Considering NSString *test = @"...href="/functions?q=KEYWORD\x26amp...";

How can I extract the word KEYWORD from a string using NSRegularExpression?
I tried using the following NSRegularExpression in iOS SDK 4.2 but could not find the text. Does the following code mean:

NSRegularExpression *testRegex = [NSRegularExpression regularExpressionWithPattern:@"(?<=href=\"\\/functions\\?q=).+?(?=\\x26amp])" options:0 error:nil];
NSRange result = [testRegex rangeOfFirstMatchInString:test options:0 range:NSMakeRange(0, [test length])];
+3
source share
1 answer

"" "]", , , . . ( , C, , ). . - . -, lookahead/lookbehind. , :

NSString *regexStr = @"href=\"\\/functions\\?=(.+?)\\\\x26amp";
NSError *error;
NSRegularExpression *testRegex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error];
if( testRegex == nil ) NSLog( @"Error making regex: %@", error );
NSTextCheckingResult *result = [testRegex firstMatchInString:test options:0 range:NSMakeRange(0, [test length])];
NSRange range = [result rangeAtIndex:1];
+10

Source: https://habr.com/ru/post/1785615/


All Articles