I got a Regex from an API. I need to validate the regex if it is valid or not.
This is a sample regex pattern:
/^[a-z0-9_-]{6,18}$/
I need to validate this string.
Edit: I have tried initwithpattern with a invalid regex [
, But its not throwing any error.
You should initialize an instance of NSRegularExpression
with the given regex pattern and check whether that NSRegularExpression
is NULL
.
NSString *regexString = @"/^[a-z0-9_-]{6,18}$/";
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:regexString
options:0
error:&error];
if (!regex) {
//regex is invalid
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.