0

Need some hints on this simple regexp.

Regexp and target string code for extracting src from < img src="..." />

NSString *imgTag = @"<img alt=\"\" src=\"/sites/default/files/mypic.gif\" style=\"width: 300px; height: 195px;\" />";

NSRegularExpression *a = [NSRegularExpression regularExpressionWithPattern:@"src=\"(.*)\"" options:NSRegularExpressionCaseInsensitive error:nil];

Not sure if it has something to do with the options

NSTextCheckingResult *matches = [a firstMatchInString:imgTag options:NSMatchingReportProgress range:NSMakeRange(0, [imgTag length])];

NSRange matchRange = [matches range];
NSString *src = [imgTag substringWithRange:matchRange];
NSLog(@"%s, %@", __PRETTY_FUNCTION__, src);

Now the output is unexpected since it returns not only the group, but also everything after it up to the end tag.

Output

/sites/default/files/mypic.gif" style="width: 300px; height: 195px;

2 Answers 2

2

It is basically matching everything from the first " to the last ".

You should probably use the non-greedy operator ?, which "Matches 0 or more times. Matches as few times as possible.".
E.g.:

NSRegularExpression *a = [NSRegularExpression regularExpressionWithPattern:@"src=\"(.*?)\"" options:NSRegularExpressionCaseInsensitive error:nil];
Sign up to request clarification or add additional context in comments.

Comments

1

Your regex is being greedy.

Try:

@"src=\"(.*?)\""

for your expression.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.