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

发布于 2024-08-25 05:14:41 字数 245 浏览 5 评论 0原文

我想创建一个正则表达式,它将根据以下条件进行匹配和返回:

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

2)我有一个文本正文。

3)我想返回用户输入的所有搜索词的所有出现次数以及周围上下文的列表。我认为 (\w+\W+){,4}(", ")(\W+\w+){,4} 可能有效。

4)我根本不知道如何使用RegexKitLite。我是否调用 RegexKitLite 类?或者它是否以某种方式连接到 NSString ?

I'd like to create a regular expression that will match and return based on the following criteria:

1) I have N search terms entered by a user

2) I have a body of text.

3) I want to return a list of all the occurrences of all the search terms entered by the user plus surrounding context. I think (\w+\W+){,4}(", ")(\W+\w+){,4} might work.

4) I don't know how to use RegexKitLite at all. Do I invoke a RegexKitLite class? or does it interface into NSString somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2024-09-01 05:14:41

RegexKitLite 定义了 类别。要获取与模式匹配的子字符串数组,请使用 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];

RegexKitLite defines a category on NSString. To get an array of substrings matching a pattern, use componentsMatchedByRegex:, as shown in the "Creating an Array of Every Match" section of the documentation.

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文