繁体   English   中英

如何使带iPhone SDK的RegexKitLite生成所有匹配项+/-的列表,并返回周围的文本

[英]How to make RegexKitLite w/ iPhone SDK produce a list of all matches +/- and also return surrounding text

我想根据以下条件创建一个匹配并返回的正则表达式:

1)我有N个用户输入的搜索字词

2)我有一段文字。

3)我想返回用户输入的所有搜索词以及周围环境的所有出现的列表。 我认为(\\ w + \\ W +){,4}(“,”)(\\ W + \\ w +){,4}可能有效。

4)我根本不知道如何使用RegexKitLite。 我是否要调用RegexKitLite类? 还是以某种方式与NSString接口?

RegexKitLite在NSString上定义了一个类别 要获取与模式匹配的子字符串数组,请使用componentsMatchedByRegex: ,如文档的“创建每个匹配的数组”部分所示。

NSArray *words = [NSArray arrayWithObjects:@"brown",@"lazy",nil];
NSString *pattern = [NSString stringWithFormat:@"(\\w+\\W+){0,4}(%@)(\\W+\\w+){0,4}",[words componentsJoinedByString:@"|"]];
NSString *text = @"the quick brown fox jumped over the lazy dog";
NSArray *matches = [text componentsMatchedByRegex:pattern];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM