-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regex.m
47 lines (41 loc) · 1.34 KB
/
Regex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// Regex.m
// swift2js
//
// Created by Gregory Casamento on 10/20/14.
// Copyright (c) 2014 swiftjs. All rights reserved.
//
#import "Regex.h"
#import <Foundation/NSRegularExpression.h>
@implementation Regex
- (id) initWithPattern: (NSString *)aPattern
{
self = [super init];
if(self != nil)
{
pattern = [aPattern copy];
regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:nil];
}
return self;
}
- (BOOL) test: (NSString *)data
{
NSTextCheckingResult *match = [regex firstMatchInString: data
options: NSMatchingReportProgress
range: NSMakeRange(0, [data length])];
return match != nil;
}
- (NSString *) firstMatch: (NSString *)data
{
NSRange range = [regex rangeOfFirstMatchInString: data
options: NSMatchingReportProgress
range: NSMakeRange(0, [data length])];
if(range.location != NSNotFound)
{
return [[data substringFromIndex: range.location] substringToIndex: range.length];
}
return nil;
}
@end