Reputation: 1612
I have NSString
as like this
/:012/:^_^/:^$^ some string
How can I extract the words which starts with /:
using Regex (NSRegularExpression
), Kindly assist. Thanks
Upvotes: 1
Views: 102
Reputation: 626699
You may use
NSString *pattern = @"/:\\S*?(?=/:|\\s|$)";
See the regex demo.
Details:
/:
- a /:
substring\S*?
- zero or more chars other than whitespace, as few as possible up to (but excluding) the first occurrence of the following:(?=/:|\s|$)
- either /:
substring, or whitespace or end of string.NSError *error = nil;
NSString *pattern = @"/:\\S*?(?=/:|\\s|$)";
NSString *string = @"/:012/:^_^/:^$^ some string";
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 matchRange = [match range];
NSString *m = [string substringWithRange:matchRange];
NSLog(@"Matched string: %@", m);
}
Results:
Matched string: /:012
Matched string: /:^_^
Matched string: /:^$^
Upvotes: 1