我正在使用正则表达式在字符串中查找某些值,但是,我试图找到的内容看起来像这样:
Dealt to SanderDecler [2s 5d]
但是我似乎找不到逃避这些方括号的方法,我之前的括号也有同样的问题。我试图逃避他们像这样\(或\ [,但没有给出任何匹配。所以我只用一个点替换它,但它确实匹配,但是,这似乎不是最好的方式做到这一点,我可以想象性能更好地指定确切的字符...
所以我的问题是,如何匹配parantheses和方括号?
以下是我的代码现在的样子,这是有效的,但不是最佳的:
NSString *expression =
@"^Dealt to (.{1,12}) .([0-9TJKQA][cdhs]) ([0-9TJKQA][cdhs]).";
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionAnchorsMatchLines
error:nil];
for (NSTextCheckingResult *result in [regex matchesInString:history options:NSMatchingReportCompletion range:NSMakeRange(0, history.length)])
{
NSLog(@"%@", [history substringWithRange:[result rangeAtIndex:0]]);
}
答案 0 :(得分:3)
试试这个:
@"^Dealt to (.{1,12}) \\[([0-9TJKQA][cdhs]) ([0-9TJKQA][cdhs])\\]"