NSRegularExpression NSInvalidValue 错误 2048

NSRegularExpression NSInvalidValue Error 2048

我正在尝试使用此 post - 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 等),基本上保持 "some subject...":

"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"

错误原因是试图转义 [ 字符

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

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

您可以跨行拆分 RE(或任何字符串),这可以使长的 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