Updatable Regular Expression Signatures

This is the idea. To have an application that does signature matching via regular expressions while also being able to update the signature remotely. This method makes exclusive use of RegexKitLite because libicucore is bundled with Mac OS X as well as the iPhone OS.

Presenting JOSignature. This version just implements NSCoding so you would have to call the serialization and deserialization yourself in order to rebuild the signature object.

#import <Foundation/Foundation.h>
#import "RegexKitLite.h"

@interface JOSignature : NSObject <NSCoding>{
    NSString *regex;
    NSDictionary *regexmap;
}

@property (readonly) NSString *regex;
@property (readonly) NSDictionary *regexmap;

-(id)initWithRegex:(NSString *)exp map:(NSDictionary *)map;
-(NSDictionary *)match:(NSString *)string;

@end

@implementation JOSignature

-(id)initWithRegex:(NSString *)exp map:(NSDictionary *)map {
    if (self = [super init]) {
        regex = [exp retain];
        regexmap = [map retain];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        regex = [[aDecoder decodeObjectForKey:@"regex"] retain];
        regexmap = [[aDecoder decodeObjectForKey:@"regexmap"] retain];      
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:regex forKey:@"regex"];
    [aCoder encodeObject:regexmap forKey:@"regexmap"];
}

-(NSDictionary *)match:(NSString *)string {
    NSArray *matchedgroups = [string arrayOfCaptureComponentsMatchedByRegex:regex];         
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    for (int i=0;i<[matchedgroups count];i++) {
        [dict setValue:[matchedgroups objectAtIndex:i] 
                forKey:[regexmap objectForKey:[NSString stringWithFormat:@”%i”,i]]];
    }
    return dict;
}
-(void)dealloc {
    [regex release];
    [regexmap release];
    [super dealloc];
}
@end
 

So how does this work? libicucore doesn’t let you do named capture groups, so we implement our own mapping dictionary to map the appropriate capture group to its property name. Additional extensions to this could be simple things like init’ing it with the remote serialized plist itself.