1

Suppose I have the following string:

bla bla bla bla i don't know what to write START name 1 END more bla bla bla bla i don't know what to write START name 2 END more bla bla bla bla i don't know what to write START name 3 END

And I want to extract the following array:

name 1

name 2

name 3

What is the best way to do it with the iOS SDK?

3 Answers 3

3

Try this:

NSArray *names = [yourString componentsSeparatedByString:@"START"];

NSArray *namesArray = [NSArray array];

for (int i = 1; i < [names count]; i++) {
    NSString *thisLine = [names objectAtIndex:i];
    NSString *name = [thisLine substringToIndex:[thisLine rangeOfString:@"END"].location];

    [namesArray addObject:name];
    NSLog(@"Your name: %@", name);
}

Just noticed you wanted this with Regex... This is not that of course, but maybe it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

indeed, this is a solution! +1 that it helps me, but i'm pretty curious how I could do it with Regex
1

Use

NSRegularExpression regularExpressionWithPattern:@"(?<=START ).*?(?= END)" 

See also Use regular expression to find/replace substring in NSString

Comments

0

To do this with regex:

(?<=START\s)(.+?)(?=\sEND)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.