Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSRegularExpression and capture groups on iphone

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

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

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 is not working the way I hope and I dont know how to capture the matched date and text. Any help would be great. BTW: there is not by any chance a regex Pattern compilation for MediaWiki Syntax out there somewhere ?

Thanks in advance Heiko *

like image 495
HeikoG Avatar asked Jan 20 '11 17:01

HeikoG


People also ask

What is a capture group?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .

What is NSRegularExpression?

An immutable representation of a compiled regular expression that you apply to Unicode strings.

What is regex in iOS?

A regex ( also known as regular expressions) is a pattern string. These pattern strings allow you to search specific patterns in documents and to validate email, phone number etc. In iOS and MacOS regex been handled by NSRegularExpression .

What is regex Swift?

Regex is a struct generic over its Output, which is the result of applying it, including captures. You can create one using a literal containing regex syntax in between slash delimiters. Swift's regex syntax is compatible with Perl, Python, Ruby, Java, NSRegularExpression, and many, many others.


2 Answers

My issue was that I was using matchesInString and I needed to use firstMatchInString because it returns multiple ranges in a single NSTextCheckingResult.

This is counter intuitive, but it worked.

I got the answer from http://snipplr.com/view/63340/

My Code (to parse 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]];
like image 100
Brenden Avatar answered Sep 22 '22 00:09

Brenden


As for the regex, i think something around these lines:

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

should work better to what you need. You're not escaping the first *, and why is there a * in the first group statement?

like image 29
Blitz Avatar answered Sep 22 '22 00:09

Blitz