0
我最近想在可可应用中使用正则表达式。但我发现可可不包含正则表达式类。所以我决定使用RegexKit(OgreKit很好,但我不知道为什么它在我的OSX 10.6.4 x86_64上运行不正常)。如何枚举RegexKit中的所有命名捕获?
我有一个文件内容,如:
12:20:30 - 01:20:30 some text 11:20:30 - 04:20:30 some text
我要拿起所有时间值和文本values.I发现了一个指南在此示例代码:
NSString *entireMatchString = NULL, *totalString = NULL, *dollarsString = NULL, *centsString = NULL;
NSString *regexString = @"owe:\\s*\\$?(?<total>(?<dollars>\\d+)\\.(?<cents>\\d+))";
[@"You owe: 1234.56 (tip not included)" getCapturesWithRegexAndReferences:regexString,@"$0", &entireString,@"${total}", &totalString,@"${dollars}", &dollarsString,@"${cents}",¢sString,nil];
// entireString = @"owe: 1234.56";
// totalString = @"1234.56";
// dollarsString = @"1234";
// centsString = @"56";
所以我写了正则表达式字符串
NSString *regexString = @"\\s*\\n*(?<start>\\d\\d:\\d\\d:\\d\\d*)\\s*-\\s*(?<end>\\d\\d:\\d\\d:\\d\\d*)\\s*\\n*(?<text>.*)
它工作正常,但它只能工作一次。我需要拿起所有命名捕获,就像我们在Ogrekit做,例如:
OGRegularExpression *regex = [OGRegularExpression regularExpressionWithString:@"<video src=\"(?<imageURL>.+)\".+>"
options:OgreCaptureGroupOption
syntax:OgreRubySyntax
escapeCharacter:OgreBackslashCharacter];
NSArray *matches = [regex allMatchesInString:@"<video src=\"http://test.com/hello.jpg\">"];
if (matches != nil && ([matches count] == 1))
{
OGRegularExpressionMatch *match = [matches objectAtIndex: 0];
NSString *result = [match substringNamed:@"ImageURL"];
// : http://test.com/hello.jpg
}
可以很容易地从比赛阵列找到所有命名捕捉值。有人知道如何在RegexKit中做什么?谢谢。
谢谢你。很有用,还有一个问题。在nsstring addtions。 RKEnumerator * matchEnumerator = [textContent matchEnumeratorWithRegex:regexString]; textContent是一个nsstring对象。我无法找到具有选项的方法mathEnumeratorXXX。所以我不能与“单行”特殊匹配选项。我应该怎么做? – findfullmoon 2010-11-10 03:54:56