The function dictionaryByMatchingRegex:withKeysAndCaptures: doesn't match the keys correctly when there are empty captures.
To repro:
1. Start with the sample code from the regexlite documentation, under "Creating a NSDictionary of URL Information".
2. Change searchString to be @"http://www.example.com/private/mail/index.html", omitting some of the optional items
3. Run
4. The expected urlDictionary result is:
{
path = "/private/mail/index.html";
protocol = http;
host = "www.example.com";
}
5. The actual urlDictionary result is:
{
password = "/private/mail/index.html";
protocol = http;
user = "www.example.com";
}
6. Confirming that the underlying search worked as expected, the result of [searchString arrayOfCaptureComponentsMatchedByRegex:regexString] is (as expected):
(
"http://www.example.com/private/mail/index.html",
http,
"",
"",
"www.example.com",
"",
"/private/mail/index.html"
)
Yes, I've encountered this as well with -arrayOfDictionariesByMatchingRegex
Regex (used as a single expression):
^.*?\\:(.*?\\@.*?)\\n
.*?\\:(.*?)\\n
.*?\\:(.*?\\@.*?)\\n
(.*?\\:(.*?\\@.*?)\\n)?
(.*?\\:(.*?\\@.*?)\\n)?
.*?\\:(.*?)(\\n.*?\\n+)
Sample input:
From: from@example.com
Date: Jan 12, 2010
To: to@example.com
Subject: subject line
Will match the subject line as capture #4, instead of #8 (expected). This pattern works as expected with -arrayOfCaptureComponentsMatchedByRegex the same way John noted above.