[英]how to get sub string using regular expression
I am trying to get the substring which is The access token provided is invalid
from 我正在尝试获取以下子字符串,即
The access token provided is invalid
Access to the requested resource path is unauthorized: v1/streaming/video/558489e46b66d1023309e1a1 [The access token provided is invalid]
What I am doing is 我在做什么
NSError *err = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[(.*)]" options:0 error:&err];
NSString *message = nil;
if (regex) {
NSTextCheckingResult *result = [regex firstMatchInString:(NSString*)error.userInfo[@"NSLocalizedDescription"]
options:0
range:NSMakeRange(0, ((NSString*)error.userInfo[@"NSLocalizedDescription"]).length)];
if ( result ) {
NSRange range = [result rangeAtIndex:1];
message = [((NSString*)error.userInfo[@"NSLocalizedDescription"]) substringWithRange:range];
}
}
However message
is nil. 但是
message
为零。 My idea is to pick up any words between []
.I think there is something wrong with my regular since I am using [(.*)]
. 我的想法是拾起
[]
之间的任何单词。由于我正在使用[(.*)]
,因此我的常规单词有误。
Does anyone have any hints on this issue. 有没有人对此问题有任何提示。
ICU User Guide: Regular Expressions ICU用户指南: 正则表达式
The regex: (?<= \\\\[)[^\\\\]]+(?=\\\\])
正则表达式:
(?<= \\\\[)[^\\\\]]+(?=\\\\])
(?<= \\\\[)
Look-behind assertion for [ which must be escaped (?<= \\\\[)
[必须转义的后置断言
[^\\\\]]+
One or more characters not ] which must be escaped [^\\\\]]+
一个或多个不是]的字符必须转义
(?=\\\\])
Look-ahead assertion for ] which must be escaped (?=\\\\])
必须转义的]的预先声明
NSString *string = @"Access to the requested resource path is unauthorized: v1/streaming/video/558489e46b66d1023309e1a1 [The access token provided is invalid]";
NSString *regex = @"(?<= \\[)[^\\]]+(?=\\])";
NSRange range = [string rangeOfString:regex options:NSRegularExpressionSearch];
NSLog(@"range: %@", NSStringFromRange(range));
NSLog(@"found: %@", [string substringWithRange:range]);
Output: 输出:
range: {100, 36}
范围:{100,36}
found: The access token provided is invalid找到:提供的访问令牌无效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.