Regex matches only a specific line

I hope to match multiple names within the document, but only matching names after a specific line.

For example, a document:

Name: Tom
Name: Alex
Name: Karina
Name: Other Names
Name: Josh
Name: Sarah
Name: Mike

So, I just want to match the names that come after “Other Names”. Estimated result - Josh, Sarah, Mike.

My current template is: (?:Other Names)[\s\S]+([A-Za-z]+)

But it returns only the last name!

+4
source share
3 answers

Objective C, , , ICU ( \G):

(?:Name:\s+Other\s+Names\s*|(?!^)\G\s*)Name:\s+(\w+)

regex

(?:Name:\s+Other\s+Names\s*|(?!^)\G\s*) Name: Other Names (?!^)\G . Name:\s+(\w+) Name: + (-) 1 ( 1 ). , .+ \w+.

Objective C:

NSError *error = nil;
NSString *pattern = @"(?:Name:\\s+Other\\s+Names\\s*|(?!^)\\G\\s*)Name:\\s+(\\w+)";
NSString *string = @"Name: Tom\nName: Alex\nName: Karina\nName: Other Names\nName: Josh\nName: Sarah\nName: Mike";
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 group1 = [match rangeAtIndex:1];
    NSLog(@"group1: %@", [string substringWithRange:group1]);
}
+1

(? <= ). *

NSString *text = @"Name: Tom Name: Alex Name: Karina Name: Other Names Name: Josh Name: Sarah Name: Mike";

NSString *pattern = @"(?<=Other Names).*";

NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

NSTextCheckingResult *match = [regularExpression firstMatchInString:text options:0 range:NSMakeRange(0, [text length])];

NSString *output = [text substringWithRange:[match rangeAtIndex:0]];
0
name: *other *names\s*([a-zA-Z ]+)

ypu , .

0

All Articles