- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
主要问题:当我的模式是 @"\\b(\\S+)\\b"
时,ObjC 可以告诉我有六个匹配项,但是当我的模式是 @ 时"A b (c) or (d)"
,它只报告一个匹配项,"c"
。
这是一个将捕获组作为 NSArray 返回的函数。我是 Objective C 新手,所以我怀疑有比创建可变数组并在末尾将其分配给 NSArray 更好的方法来完成笨拙的工作。
- (NSArray *)regexWithResults:(NSString *)haystack pattern:(NSString *)strPattern
{
NSArray *ar;
ar = [[NSArray alloc] init];
NSError *error = NULL;
NSArray *arTextCheckingResults;
NSMutableArray *arMutable = [[NSMutableArray alloc] init];
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:strPattern
options:NSRegularExpressionSearch error:&error];
arTextCheckingResults = [regex matchesInString:haystack
options:0
range:NSMakeRange(0, [haystack length])];
for (NSTextCheckingResult *ntcr in arTextCheckingResults) {
int captureIndex;
for (captureIndex = 1; captureIndex < ntcr.numberOfRanges; captureIndex++) {
NSString * capture = [haystack substringWithRange:[ntcr rangeAtIndex:captureIndex]];
//NSLog(@"Found '%@'", capture);
[arMutable addObject:capture];
}
}
ar = arMutable;
return ar;
}
我习惯于在 Perl 中使用括号来匹配捕获组,方式如下:
#!/usr/bin/perl -w
use strict;
my $str = "This sentence has words in it.";
if(my ($what, $inner) = ($str =~ /This (\S+) has (\S+) in it/)) {
print "That $what had '$inner' in it.\n";
}
该代码将产生:
That sentence had 'words' in it.
But in Objective C, with NSRegularExpression, we get different results. Sample function:
- (void)regexTest:(NSString *)haystack pattern:(NSString *)strPattern
{
NSError *error = NULL;
NSArray *arTextCheckingResults;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:strPattern
options:NSRegularExpressionSearch
error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:haystack options:0 range:NSMakeRange(0, [haystack length])];
NSLog(@"Pattern: '%@'", strPattern);
NSLog(@"Search text: '%@'", haystack);
NSLog(@"Number of matches: %lu", numberOfMatches);
arTextCheckingResults = [regex matchesInString:haystack options:0 range:NSMakeRange(0, [haystack length])];
for (NSTextCheckingResult *ntcr in arTextCheckingResults) {
NSString *match = [haystack substringWithRange:[ntcr rangeAtIndex:1]];
NSLog(@"Found string '%@'", match);
}
}
调用该测试函数,结果显示它能够计算字符串中的单词数:
NSString *searchText = @"This sentence has words in it.";
[myClass regexTest:searchText pattern:@"\\b(\\S+)\\b"];
Pattern: '\b(\S+)\b' Search text: 'This sentence has words in it.' Number of matches: 6 Found string 'This' Found string 'sentence' Found string 'has' Found string 'words' Found string 'in' Found string 'it'
But what if the capture groups are explicit, like so?
[myClass regexTest:searchText pattern:@".*This (sentence) has (words) in it.*"];
结果:
Pattern: '.*This (sentence) has (words) in it.*' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence'
Same as above, but with \S+ instead of the actual words:
[myClass regexTest:searchText pattern:@".*This (\\S+) has (\\S+) in it.*"];
结果:
Pattern: '.*This (\S+) has (\S+) in it.*' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence'
How about a wildcard in the middle?
[myClass regexTest:searchText pattern:@"^This (\\S+) .* (\\S+) in it.$"];
结果:
Pattern: '^This (\S+) .* (\S+) in it.$' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence'
引用资料: NSRegularExpression NSTextCheckingResult NSRegularExpression matching options
最佳答案
我想如果你改变
// returns the range which matched the pattern
NSString *match = [haystack substringWithRange:ntcr.range];
到
// returns the range of the first capture
NSString *match = [haystack substringWithRange:[ntcr rangeAtIndex:1]];
对于包含单个捕获的模式,您将获得预期的结果。
请参阅 NSTextCheckingResult:rangeAtIndex 的文档页面:
结果必须至少有一个范围,但也可以有更多范围(例如,表示正则表达式捕获组)。
传递 rangeAtIndex:值 0 始终返回 range 属性的值。其他范围(如果有)的索引将从 1 到 numberOfRanges-1。
关于objective-c - 为什么 NSRegularExpression 在所有情况下都不支持捕获组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7600103/
我有一个像这样的字符串: ?key=123%252Bf-34Fa&name=John ?name=Johon&key=123%252Bf-34Fa 我想获取key的值,我使用此NSRegularExp
我曾使用 NSScanner 从下面的文本中检索数字。但有时结果并不像预期的那样。听说 iOS 4 可用的 NSRegularExpression 类更适合做这种类型的提取。由于我是 NSRegula
全部, 我有一本包含两个键和值的字典。我需要从它们中提取一些片段并将它们放入单独的字符串中。 { IP = "192.168.17.1"; desc = "VUWI-V
我想用 iPhone 的 NSRegularExpression 类解析此文本: Uploaded652.81 GB 用于摘录上传和652.81文本。 最佳答案 虽然我确实认为 xml 解析器更适合解
嗨,我遇到了问题,如果字符串中没有电话号码,并且 NSRegularExpression 找不到任何内容,则应用程序崩溃,但当它确实找到电话号码时,它在字符串中工作正常,没有任何问题。我怎样才能阻止它
我的代码看起来像 NSString *pattern = @"\\w+(\\w)"; NSRegularExpression *regex = [NSRegularExpression
我在理解使用 NSRegularExpression 获取捕获组时遇到了一些问题 let string = "QWERTY123 ASDFGH" let nsString = string as NS
我有一个正在 UIWebView 中显示的 HTML 字符串。有一些希腊文本,Unicode 范围 U+0370–U+03FF,我想通过将其括在跨度中以不同的字体显示。我可以搜索\p{script=G
我正在尝试更改字符的颜色和任何文本,我正在使用 NSRegularExpression 来获取字符,但我不知道如何更改以下字符,例如这是我的文本: ● Title...... some descrip
我正在编写一个通过 TCP 接收消息 block 的应用程序。消息 block 由以下部分组成: 固定 header <<:--!! 给出消息长度的 6 位数字 消息本身 使用 NSRegularEx
我知道 NSBackwardSearch 用于向后搜索字符串。 我的问题是,我可以通过 NSRegularExpression 进行相同的反向搜索吗? 如果是,请分享您的知识? 最佳答案 NSRegu
我想将 stringWithHashtags 中的每个单词添加到以 @ 和 # 开头的 attString 中.我认为最简单的方法是合并两个 NSRegularExpression 然后将其分配给数组
所以我正在开发一个 iPhone 应用程序,它拍摄一些文本的图片,图片经过 OCR 处理并发回给我,然后我使用正则表达式在字符串中搜索 double 值,最多可达xxxx.xx。 NSRegularE
我必须在 iOS 应用程序上动态替换 HTML 文件。 这与本地化有关。 在 HTML 文件中,我有类似这样的内容:LANG(183) 必须将其转换为“本地化字符串” 我正在查看 stringByre
我想使用NSRegularExpression获取html中的所有下载链接。 例如html内容是这样的: text info 我想得到这样的所有字符串: href="http://xxxx.com/f
我想让 TextView 中的匹配项为蓝色。 NSString *text = @"Except as contained in #wep this notice, the name of a cop
我使用 NSRegularExpression 来查找与特定模式匹配的内容,该模式在代码片段中可见: - (NSString *)functionPattern { return @"[A-Z
我正在尝试编写一个函数,它有一个 NSString 并解析它返回一个标签数组。 标签的定义是任何以# 开头并且在# 之后仅包含字母数字字符的 nsstring 文本。 这是正确的吗? #.*?[A-Z
我有以下网址: https://api.instagram.com/v1/tags/picoftheday/media/recent?access_token=29088696%2Efd3325f%2
您好,我在 NSRegularExpressions 中使用正则表达式的非捕获组功能时遇到问题 下面是一些捕获匹配的代码: func matches(for regex: String, in tex
我是一名优秀的程序员,十分优秀!