获取前后单词的正则表达式

3

我想得到我用 正则表达式 搜索到的单词的前一个和后一个单词,可以使用以下模式来获得。

在这个例子中,我已经搜索了单词"the",所以我可以使用以下模式成功地获取"the"的前一个和后一个单词。

'\\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)'

我希望使用一个模式来涵盖所有三种情况,无论单词是在开头、中间还是结尾。

已经尝试过多种组合,但没有成功。

请问有谁能帮我把这三种情况都包含在一个模式中,并且可以给出前/后面的单词结果吗?

2个回答

2
您可以使用以下两种正则表达式来匹配字符串中的 cite 关键字: (\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

匹配结果:
  • any
  • materials
  • that
  • attachments
请参见 DEMO

好的,让我检查一下修改后的答案。感谢您的反馈。 - Niks
演示看起来很完美,但是当我在我的iOS应用程序中尝试时,无论单词在哪里,都只会给出前一个单词。 - Niks
尝试这个.. (\w+)?\s*cite\s*(\w+)? - karthik manchala
是的,它幾乎完成了。非常感謝,但仍然有一個小問題,例如,我正在測試搜索“the”,它還給我返回了包含“other”的“the”結果。如果存在特定單詞,它應只給出該單詞。你能幫忙解決這個問題嗎?謝謝,非常感謝你的幫助! - Niks
让我们在聊天室里继续这个讨论:http://chat.stackoverflow.com/rooms/74839/discussion-between-karthik-manchala-and-niks。 - karthik manchala
显示剩余2条评论

1

我认为你可以使用以下正则表达式来捕获所有内容,它使用可选的捕获组,不需要使用替代方案:

(\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  

谢谢,但是这里有一个问题,它没有返回页面中第一个单词。 - Niks
嗨,我检查了一下。正如您所看到的,它会从字符串的开头、中间和结尾返回“cite”三次。 - Wiktor Stribiżew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接