正则表达式获取下一个上一个单词
REGEX to get Next Previous word
我想要用 REGEX 搜索的下一个和上一个词,我可以用下面的模式得到它。
在这种情况下,我已经搜索了单词 "the",所以我可以得到 "the"[=42 的下一个和上一个单词=].我可以通过以下模式成功获得。
'\b(?=(\w+\s+the|the\s+\w+)\b)'
但是 对于这种模式,我遇到一个问题是当搜索的词在页面中排在第一位("cite" 在下面的示例文本的情况下)或最后("attachments" 在下面的示例文本的情况下)它不会找到它。
示例文本
cite any cases or other legal materials that the arbitrator should read before the hearing attachments
我也得到第一个词和最后一个词,但模式不同。
搜索词在页面中排在第一位时的模式。
第一个单词
'\b(?=($+cite|cite\s+\w+)\b)'
最后一句话
'\b(?=(\w+\s+attachments|attachments+$)\b)'
我想要所有这三种可能性,单一模式天气词是第一个或最后一个或中间。
已测试更改组合,但未成功。
谁能帮我把所有这些都放在一个模式中,就像它应该给出 next/previous 个单词的结果一样?
您可以使用:(\w+)?\s+cite(\s+\w+)?|cite\s+(\w+)?
和 (\w+)?\s*\bcite\b\s*(\w+)?
(假设 cite
标记作为示例词)
示例字符串:
cite any cases or other legal materials cite that the arbitrator should read before the hearing attachments cite
匹配项:
- 任意
- 材料
- 那个
- 附件
见DEMO
我认为您可以使用以下使用可选捕获组的正则表达式捕获所有内容,无需使用交替:
(\w+)?\s*\b(cite)\b\s*(\w+)?
不要忘记在 Objective C 中使用双转义斜杠。
#import <Foundation/Foundation.h>
#import <Foundation/NSTextCheckingResult.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
NSString *pattern = @"(\w+)?\s*\bcite\b\s*(\w+)?";
NSString *string = @"cite any cases or other legal materials cite that the arbitrator should read before the hearing attachments cite";
NSRange range = NSMakeRange(0, string.length);
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:range];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NSString *m = [string substringWithRange:matchRange];
NSLog(@"Matched string: %@", m);
}
[pool drain];
return 0;
}
输出:
2015-04-09 11:08:22.630 main[26] Matched string: cite any
2015-04-09 11:08:22.633 main[26] Matched string: materials cite that
2015-04-09 11:08:22.633 main[26] Matched string: attachments cite
我想要用 REGEX 搜索的下一个和上一个词,我可以用下面的模式得到它。
在这种情况下,我已经搜索了单词 "the",所以我可以得到 "the"[=42 的下一个和上一个单词=].我可以通过以下模式成功获得。
'\b(?=(\w+\s+the|the\s+\w+)\b)'
但是 对于这种模式,我遇到一个问题是当搜索的词在页面中排在第一位("cite" 在下面的示例文本的情况下)或最后("attachments" 在下面的示例文本的情况下)它不会找到它。
示例文本
cite any cases or other legal materials that the arbitrator should read before the hearing attachments
我也得到第一个词和最后一个词,但模式不同。 搜索词在页面中排在第一位时的模式。
第一个单词
'\b(?=($+cite|cite\s+\w+)\b)'
最后一句话
'\b(?=(\w+\s+attachments|attachments+$)\b)'
我想要所有这三种可能性,单一模式天气词是第一个或最后一个或中间。
已测试更改组合,但未成功。
谁能帮我把所有这些都放在一个模式中,就像它应该给出 next/previous 个单词的结果一样?
您可以使用:(\w+)?\s+cite(\s+\w+)?|cite\s+(\w+)?
和 (\w+)?\s*\bcite\b\s*(\w+)?
(假设 cite
标记作为示例词)
示例字符串:
cite any cases or other legal materials cite that the arbitrator should read before the hearing attachments cite
匹配项:
- 任意
- 材料
- 那个
- 附件
见DEMO
我认为您可以使用以下使用可选捕获组的正则表达式捕获所有内容,无需使用交替:
(\w+)?\s*\b(cite)\b\s*(\w+)?
不要忘记在 Objective C 中使用双转义斜杠。
#import <Foundation/Foundation.h>
#import <Foundation/NSTextCheckingResult.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
NSString *pattern = @"(\w+)?\s*\bcite\b\s*(\w+)?";
NSString *string = @"cite any cases or other legal materials cite that the arbitrator should read before the hearing attachments cite";
NSRange range = NSMakeRange(0, string.length);
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:range];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NSString *m = [string substringWithRange:matchRange];
NSLog(@"Matched string: %@", m);
}
[pool drain];
return 0;
}
输出:
2015-04-09 11:08:22.630 main[26] Matched string: cite any
2015-04-09 11:08:22.633 main[26] Matched string: materials cite that
2015-04-09 11:08:22.633 main[26] Matched string: attachments cite