-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFIRegexMediator.m
98 lines (85 loc) · 3.14 KB
/
RFIRegexMediator.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// Created by Annard Brouwer on 27/10/2010.
// Copyright (c) 2010 Let Software Dream Ltd., All Rights Reserved.
//
#import <OSAKit/OSAScript.h>
#import "RegexKitLite.h"
#import "RFIRegexMediator.h"
@implementation RFIRegexMediator
+ (NSString *)validateRegularExpression:(NSString *)exprStr
{
NSString *retStr;
retStr = @"OK";
@try
{
NSString *testStr;
NSError *regexErr;
NSRange result;
testStr = @"TestMe";
result = [testStr RKL_METHOD_PREPEND(rangeOfRegex): exprStr
options: RKLNoOptions
inRange: NSMakeRange(0, [testStr length])
capture: 0
error: ®exErr];
if (NSNotFound == result.location && 0 == result.length && regexErr)
{
retStr = [NSString stringWithFormat: @"%@ [at character location: %@]", [regexErr localizedDescription],
[[regexErr userInfo] objectForKey: RKLICURegexOffsetErrorKey]];
}
}
@catch (NSException *nse)
{
retStr = [nse reason];
}
return retStr;
}
+ (NSString *)find:(NSString *)findStr
replace:(NSString *)replaceStr
inText:(NSString *)txtStr
useRegEx:(BOOL)useRegEx
{
NSString *resultStr;
resultStr = nil;
@try
{
if (useRegEx)
resultStr = [txtStr RKL_METHOD_PREPEND(stringByReplacingOccurrencesOfRegex): findStr withString: replaceStr];
else
resultStr = [txtStr stringByReplacingOccurrencesOfString: findStr withString: replaceStr];
}
@catch (id nse)
{
NSDictionary *errorInfo;
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: errOSASystemError], OSAScriptErrorNumber, [nse reason], OSAScriptErrorMessage, nil];
NSLog(@"[%@] Exception with %@ find and replace: %@", (useRegEx) ? @"regex" : @"regular",
[[[NSBundle mainBundle] localizedInfoDictionary] objectForKey: (id)kCFBundleNameKey],
[nse description]);
@throw [NSException exceptionWithName: [nse name]
reason: [nse reason]
userInfo: errorInfo];
}
if (nil == resultStr)
resultStr = txtStr;
return resultStr;
}
+ (NSNumber *)regularExpression:(NSString *)exprStr
isMatchedByString:(NSString *)findStr
{
BOOL isMatch;
isMatch = NO;
@try
{
isMatch = [findStr RKL_METHOD_PREPEND(isMatchedByRegex): exprStr];
}
@catch (id nse)
{
NSDictionary *errorInfo;
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: errOSASystemError], OSAScriptErrorNumber, [nse reason], OSAScriptErrorMessage, nil];
NSLog(@"[%@] Exception with finding a match: %@", [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey: (id)kCFBundleNameKey], [nse description]);
@throw [NSException exceptionWithName: [nse name]
reason: [nse reason]
userInfo: errorInfo];
}
return [NSNumber numberWithBool: isMatch];
}
@end