如何在 Objective C 中编写正则表达式(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.tld2”。 tld.tld2"
-- 更新 2 --
此正则表达式从字符串中提取“域名”和“TLD”:
- example.com = (example, com)
- example.co.uk = (example, co.uk)
- -test-example.co.u =(测试示例,co)
- -test-example.co.uk- =(测试示例,co.uk)
- -test-example.co.uk =(测试示例,co )
- -test-example.co-m = (test-example)
- -test-example-.co.uk = (test-example)
它采用有效的域名(不以“-”开头或结尾且介于 2 和 63 之间 )个字符长),并且最多包含 TLD 的两个部分,如果这些部分有效(至少两个字符长,仅包含字母和数字)
I have this regex working when I test it in PHP but it doesn't work in Objective C:
(?:www\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\.?((?:[a-zA-Z0-9]{2,})?(?:\.[a-zA-Z0-9]{2,})?)
I tried escaping the escape characters but that doesn't help either. Should I escape any other character?
This is my code in 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);
}
-- UPDATE --
This regex returns (in PHP) the array with values "domain-name" and "tld.tld2" but in Objective C i get only one value: "domain-name.tld.tld2"
-- UPDATE 2 --
This regex extracts 'domain name' and 'TLD' from the string:
- example.com = (example, com)
- example.co.uk = (example, 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)
it takes the valid domain name (not starting or ending with '-' and between 2 and 63 characters long), and up to two parts of a TLD if the parts are valid (at least two characters long containing only letters and numbers)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSTextCheckingResult
具有通过索引获得的多个项目。[match rangeAtIndex:0];
是完全匹配。[match rangeAtIndex:1];
(如果存在)是第一个捕获组匹配。等等。
你可以使用这样的东西:
NSLog 输出:
测试匹配范围是否有效。
在这种情况下更简单:
打印输出:
在这种情况下更简单:
打印输出:
A
NSTextCheckingResult
has multiple items obtained by indexing into it.[match rangeAtIndex:0];
is the full match.[match rangeAtIndex:1];
(if it exists) is the first capture group match.etc.
You can use something like this:
NSLog output:
Do test that the match ranges are valid.
More simply in this case:
print output:
More simply in this case:
print output:
根据 Apple 文档,这些字符必须用引号引起来(使用 \)才能被视为文字:
如果您能解释您想要实现的目标,这也会有所帮助。你有测试装置吗?
According to Apple's documentation, these characters must be quoted (using \) to be treated as literals:
It would also help if you could explain what you are trying to achieve. Do you have any test fixtures?