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

标签 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”:

  • example.com = (example, com)
  • example.co.uk =(例如,co.uk)
  • -test-example.co.u = (test-example, co)
  • -test-example.co.uk- = (test-example, co.uk)
  • -test-example.co.u-k = (test-example, co)
  • -test-example.co-m = (test-example)
  • -test-example-.co.uk = (test-example)

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

最佳答案

一个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 输出:

match: domain-name.tld.tld2
domain-name
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)
}

打印输出:

match: domain-name.tld.tld2
matchText1: domain-name
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: domain-name
matchText2: tld.tld2

关于objective-c - 如何在Objective C(NSRegularExpression)中编写正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9276246/

相关文章:

iphone - 在自定义 UITableViewCell 中访问 UITextField

iphone - Objective-C 中的排列/字谜——我遗漏了一些东西

ios - UITabBarView 使图像变成圆形

objective-c - 通过OSX Cron运行时,Objective C命令失败

ios - 如何清除/删除 iCloud 备份数据?

ios - 如何以编程方式在 UITableViewCell 中添加 UIView?

iphone - Xcode iOS 模拟器的设置 > 蓝牙只是旋转但没有打开

java - 正则表达式替换字符实例,但不在括号内

python - 将正则表达式转换为有限状态机

regex - 如何在 Go 正则表达式中获取捕获组功能