如何在Objective C(NSRegularExpression)中编写正则表达式?

时间:2012-02-14 11:43:37

标签: objective-c ios regex nsregularexpression

当我在PHP中测试它时,我有这个正则表达式,但它在Objective C中不起作用:

(?:www\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\.?((?:[a-zA-Z0-9]{2,})?(?:\.[a-zA-Z0-9]{2,})?)

我尝试转义转义字符,但这也无济于事。我应该逃避任何其他角色吗?

这是我在Objective C中的代码:

NSMutableString *searchedString = [NSMutableString stringWithString:@"domain-name.tld.tld2"];
NSError* error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)" options:0 error:&error];
NSArray* matches = [regex matchesInString:searchedString options:0 range:NSMakeRange(0, [searchedString length])];
for ( NSTextCheckingResult* match in matches )
{
    NSString* matchText = [searchedString substringWithRange:[match range]];
    NSLog(@"match: %@", matchText);
}

- 更新 -

此正则表达式返回(在PHP中)值为“domain-name”和“tld.tld2”的数组,但在Objective C中,我只得到一个值:“domain-name.tld.tld2”

- 更新2 -

此正则表达式从字符串中提取“域名”和“TLD”:

  • domain.com =(domain,com)
  • domain.co.uk =(domain,co.uk)
  • -test-domain.co.u =(test-domain,co)
  • -test-domain.co.uk- =(test-domain,co.uk)
  • -test-domain.co.u-k =(test-domain,co)
  • -test-domain.co-m =(test-domain)
  • -test-domain-.co.uk =(test-domain)

它采用有效的域名(不以' - '开头或结尾,长度在2到63个字符之间),如果部分有效,则最多为TLD的两个部分(至少两个字符只包含字母和数)

希望这个解释有所帮助。

2 个答案:

答案 0 :(得分:76)

NSTextCheckingResult通过索引获得多个项目。

[match rangeAtIndex:0];是完全匹配 [match rangeAtIndex:1];(如果存在)是第一个捕获组匹配 等。

您可以使用以下内容:

NSString *searchedString = @"domain-name.tld.tld2";
NSRange   searchedRange = NSMakeRange(0, [searchedString length]);
NSString *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)";
NSError  *error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
NSArray* matches = [regex matchesInString:searchedString options:0 range: searchedRange];
for (NSTextCheckingResult* match in matches) {
    NSString* matchText = [searchedString substringWithRange:[match range]];
    NSLog(@"match: %@", matchText);
    NSRange group1 = [match rangeAtIndex:1];
    NSRange group2 = [match rangeAtIndex:2];
    NSLog(@"group1: %@", [searchedString substringWithRange:group1]);
    NSLog(@"group2: %@", [searchedString substringWithRange:group2]);
}

NSLog输出:

  

匹配:domain-name.tld.tld2
  域名
  tld.tld2

测试匹配范围是否有效。

在这种情况下更简单:

NSString *searchedString = @"domain-name.tld.tld2";
NSRange   searchedRange = NSMakeRange(0, [searchedString length]);
NSString *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)";
NSError  *error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:searchedString options:0 range: searchedRange];
NSLog(@"group1: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]);
NSLog(@"group2: %@", [searchedString substringWithRange:[match rangeAtIndex:2]]);
  

Swift 3.0:

let searchedString = "domain-name.tld.tld2"
let nsSearchedString = searchedString as NSString
let searchedRange = NSMakeRange(0, searchedString.characters.count)
let pattern = "(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)"

do {
    let regex = try NSRegularExpression(pattern:pattern, options: [])
    let matches = regex.matches(in:searchedString, options:[], range:searchedRange)
    for match in matches {
        let matchText = nsSearchedString.substring(with:match.range);
        print("match: \(matchText)");

        let group1 : NSRange = match.rangeAt(1)
        let matchText1 = nsSearchedString.substring(with: group1)
        print("matchText1: \(matchText1)")

        let group2 = match.rangeAt(2)
        let matchText2 = nsSearchedString.substring(with: group2)
        print("matchText2: \(matchText2)")
    }
} catch let error as NSError {
    print(error.localizedDescription)
}

打印输出:

  

匹配:domain-name.tld.tld2
  matchText1:域名
  matchText2:tld.tld2

在这种情况下更简单:

do {
    let regex = try NSRegularExpression(pattern:pattern, options: [])
    let match = regex.firstMatch(in:searchedString, options:[], range:searchedRange)

    let matchText1 = nsSearchedString.substring(with: match!.rangeAt(1))
    print("matchText1: \(matchText1)")

    let matchText2 = nsSearchedString.substring(with: match!.rangeAt(2))
    print("matchText2: \(matchText2)")

} catch let error as NSError {
    print(error.localizedDescription)
}

打印输出:

  

matchText1:域名
  matchText2:tld.tld2

答案 1 :(得分:14)

根据Apple's documentation,必须引用这些字符(使用\)作为文字处理:

* ? + [ ( ) { } ^ $ | \ . /

如果你能解释一下你想要实现的目标,那也会有所帮助。你有任何测试设备吗?