NSRegularExpression用于检测iOS中的em-dash

adi*_*dit 0 iphone objective-c ipad ios

我目前有以下正则表达式:

static inline NSRegularExpression * AuthorRegularExpression() {
    if (!__authorRegularExpression) {
        __authorRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"-\\s*(.)*$" options:NSRegularExpressionCaseInsensitive error:nil];
    }

    return __authorRegularExpression;
}
Run Code Online (Sandbox Code Playgroud)

正如您在模式中看到的那样,它会检测到一个短划线( - ),后跟一些字符串.但是现在我已经将短划线的格式更改为em-dash.所以字符串如下所示:

  NSString *dashAuthor = [NSString stringWithFormat:@"%C %@", 0x2014, self.theme.quoteAuthor];
Run Code Online (Sandbox Code Playgroud)

我现在如何更改正则表达式以反映这一点,因此它可以找到一个带有尾随em-dash的作者.

omz*_*omz 6

您可以将em破折号与其unicode值(\u2014)或字符名称匹配,如下所示:\N{EM DASH}.