NSRegularExpression NSInvalidValue错误2048

时间:2015-01-29 21:14:28

标签: objective-c regex cocoa

我正在尝试使用此帖子中描述的任何正则表达式 - Regex/code for removing "FWD", "RE", etc, from email subject

例如,

NSString *pattern = @"([\[\(] *)?(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|ΑΠ|ΣΧΕΤ|ΠΡΘ|תגובה|הועבר|主题|转发|FWD?) *([-:;)\\]][ :;\\])-]*|$)|\\]+ *$";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

NSRange textRange = NSMakeRange(0, workingSubject.length);
NSString *newSubject = [regex stringByReplacingMatchesInString:workingSubject options:0 range:textRange withTemplate:@""];

但我总是收到此错误

Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)"
UserInfo=0x7fb6cde65530 {NSInvalidValue=([[(] *)?(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|ΑΠ|ΣΧΕΤ|ΠΡΘ|תגובה|הועבר|主题|转发|FWD?) *([-:;)]][ :;])-]*|$)|\]+ *$}

我该怎么做才能解决这个问题?

作为参考,我也尝试过这些表达式,但错误相同:

 /([\[\(] *)?(RE|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$/igm

 ([\[\(] *)?(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|ΑΠ|ΣΧΕΤ|ΠΡΘ|תגובה|הועבר|主题|转发|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$'

 /([\[\(] *)?(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|ΑΠ|ΣΧΕΤ|ΠΡΘ|תגובה|הועבר|主题|转发|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$/im

以下是一些主题示例,它应该删除主要修饰符(Re,Fw,Fwd,Aw等),基本上保留"某些主题......":

"Re: re: some subject asdf;'[]"
"Re: re: some subject asdf;'()"
"Re: re: some subject 12345678910"
"Re: re: some subject !@#$%^&*()_+`~"
"Re: re: some subject <>?:\"{},./;'[]|\\=-'"
"Re: re: some subject regarding"
"Fw: re: some subject regarding fwoo"
"Fw: FWD: some subject regarding fwoo"
"FWD: fw: some subject regarding fwoo"
"Fwd: RE: some subject regarding fwoo"
"Fwd: FWD: RE: re: some subject regarding fwoo"
"Aw: FWD: RE: re: some subject regarding fwoo"

1 个答案:

答案 0 :(得分:1)

错误的原因是试图逃避[character

Incorrect: `NSString *pattern = @"([\[\(] *)?`  
Corrected: `NSString *pattern = @"([\\[(] *)?`  

Inside a character set only [ ] and \ need escaping.
Outside a character set only * ? + [ ( ) { } ^ $ | \ . need escaping.

你可以在一行中拆分RE(或任何字符串),这可以为长篇文章带来一些清晰度:

NSString *pattern = @
    "([\\[(] *)?" // leading [ or ( followed by optional spaces zero or one times
    "(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|ΑΠ|ΣΧΕΤ|ΠΡΘ|תגובה|הועבר|主题|转发|FWD?) *"
    "([-:;)\\]][ :;\\])-]*|$)"
        "|"
    "\\]+ *$";

我针对示例运行了RE:

NSArray *subjectList = @[
     @"Re: re: some subject asdf;'[]",
     @"Re: re: some subject asdf;'()",
     @"Re: re: some subject 12345678910",
     @"Re: re: some subject !@#$%^&*()_+`~",
     @"Re: re: some subject <>?:\"{},./;'[]|\\=-'",
     @"Re: re: some subject regarding",
     @"Fw: re: some subject regarding fwoo",
     @"Fw: FWD: some subject regarding fwoo",
     @"FWD: fw: some subject regarding fwoo",
     @"Fwd: RE: some subject regarding fwoo",
     @"Fwd: FWD: RE: re: some subject regarding fwoo",
     @"Aw: FWD: RE: re: some subject regarding fwoo"
     ];

NSString *pattern = @
"([\\[(] *)?" // leading [ or ( followed by optional spaces zero or one times
"(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|ΑΠ|ΣΧΕΤ|ΠΡΘ|תגובה|הועבר|主题|转发|FWD?) *"
"([-:;)\\]][ :;\\])-]*|$)"
"|"
"\\]+ *$";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
for (NSString *subject in subjectList) {
    NSRange   subjectRange = NSMakeRange(0, subject.length);
    NSString *newSubject = [regex stringByReplacingMatchesInString:subject options:0 range:subjectRange withTemplate:@""];
    NSLog(@"newSubject: %@", newSubject);
}

输出:

newSubject: some subject asdf;'[
newSubject: some subject asdf;'()
newSubject: some subject 12345678910
newSubject: some subject !@#$%^&*()_+`~
newSubject: some subject ?:"{},./;'[]|\=-'
newSubject: some subject regarding
newSubject: some subject regarding fwoo
newSubject: some subject regarding fwoo
newSubject: some subject regarding fwoo
newSubject: some subject regarding fwoo
newSubject: some subject regarding fwoo
newSubject: some subject regarding fwoo