gpt4 book ai didi

ios - 使用正则表达式提取两个标记之间的文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:09 24 4
gpt4 key购买 nike

简单的正则表达式问题。我有以下格式的字符串:

[page]
Some text with multi line.
[page/]

[page]
Another text with multi line.
[page/]

[page]
Third text with multi line.
[page/]

提取[page][page/]之间文本的正则表达式是什么?

我正在使用这段代码,但我只得到了第一个匹配项。

NSString *path = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"];
NSString *mainText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

NSError *error = NULL;
NSRange range = NSMakeRange(0, mainText.length);

NSString *pattern = [NSString stringWithFormat:@"(?<=\\[page])(?s)(.*?)(?=\\[page/])"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:mainText options:0 range:range];


if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
NSString *substringForFirstMatch = [mainText substringWithRange:rangeOfFirstMatch];
NSLog(@"sub: %@", substringForFirstMatch);
}

除了 NSArray 中每个匹配项的文本,我怎样才能做到这一点?

最佳答案

您可以使用 matchesInString:options:range:,它返回一个匹配数组作为 NSTextCheckingResults:

    NSString *pattern = [NSString stringWithFormat:@"(?<=\\[page\\])(.*?)(?=\\[page\\/\\])"];
NSUInteger options = NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:options error:&error];

for (NSTextCheckingResult* result in [regex matchesInString:INPUT_STRING
options:0
range:NSMakeRange(0, [input_string_length])])
{
// further code
}

关于ios - 使用正则表达式提取两个标记之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29050504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com