51

当我在 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 =(域,com)
  • domain.co.uk = (域名, co.uk)
  • -test-domain.co.u =(测试域,co)
  • -test-domain.co.uk- =(测试域,co.uk)
  • -test-domain.co.uk = (test-domain, co)
  • -test-domain.co-m = (测试域)
  • -test-domain-.co.uk =(测试域)

它需要有效的域名(不以“-”开头或结尾,并且长度在 2 到 63 个字符之间),如果部分有效,则最多包含 TLD 的两个部分(至少两个字符长,仅包含字母和数字)

希望这个解释有所帮助。

4

2 回答 2

83

ANSTextCheckingResult有多个通过索引获得的项目。

[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 输出:

匹配:域名.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]]);

斯威夫特 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)
}

打印输出:

匹配:域名.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

于 2012-02-14T12:24:46.623 回答
17

根据Apple 的文档,这些字符必须被引用(使用 \)才能被视为文字:

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

如果你能解释你想要达到的目标,这也会有所帮助。你有测试夹具吗?

于 2012-02-14T11:52:46.790 回答