NSRegularExpression and Capture Groups on iphone

I need a little kickstart on regex on iphone. I basically have a date list in a private MediaWiki in the form *185 BC: SOME EVENT HERE
*2001: SOME OTHER EVENT MUCH LATER

Now I want to parse this into an Object that has an NSDate property and a -say-NSString property. I still have this: (rawContentString contains the mediawiki page syntax)

 NSString* regexString =@ "\\*( *[0-9]{1,}.*): (.*)"; NSRegularExpressionOptions options = NSRegularExpressionCaseInsensitive; NSError* error = NULL; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:regexString options:options error:&error]; if (error) { NSLog(@"%@", [error description]); } NSArray* results = [regex matchesInString:rawContentString options:0 range:NSMakeRange(0, [rawContentString length])]; for (NSTextCheckingResult* result in results) { NSString* resultString = [rawContentString substringWithRange:result.range]; NSLog(@"%@",resultString); } 

Unfortunately, I think the regex doesn't work the way I hope, and I don't know how to write down an agreed date and text. Any help would be great. By the way: is there any way to compile a regular expression for MediaWiki syntax somewhere?

Thanks in advance Heiko *

+4
source share
2 answers

Regarding regex, I think something around these lines:

 \*([ 0-9]{1,}.*):(.*) 

should work better than you need. You do not avoid the first *, and why is there * in the first statement of the group?

+2
source

My problem was that I used matchesInString and I needed to use firstMatchInString because it returns multiple ranges in the same NSTextCheckingResult .

This counter is intuitive, but it worked.

I got a response from http://snipplr.com/view/63340/

My code (for analyzing credit card track data):

 NSRegularExpression *track1Pattern = [NSRegularExpression regularExpressionWithPattern:@"%.(.+?)\\^(.+?)\\^([0-9]{2})([0-9]{2}).+?\\?." options:NSRegularExpressionCaseInsensitive error:&error]; NSTextCheckingResult *result = [track1Pattern firstMatchInString:trackString options:NSMatchingReportCompletion range:NSMakeRange(0, trackString.length)]; self.cardNumber = [trackString substringWithRange: [result rangeAtIndex:1]]; self.cardHolderName = [trackString substringWithRange: [result rangeAtIndex:2]]; self.expirationMonth = [trackString substringWithRange: [result rangeAtIndex:3]]; self.expirationYear = [trackString substringWithRange: [result rangeAtIndex:4]]; 
+2
source

Source: https://habr.com/ru/post/1336347/


All Articles