In iOS how to check string against regular expression with boolean result

new2ios

In iOS I need to check string against regular expression and if it passes then to return true (for example), if not false. I understand that I have to use NSRegularExpression class, but I can not figure out how.

Oleksandr Karaberov

You should read documentation.

Here is an example code how to do this in general:

- (BOOL)checkString:(NSString *)string {

    NSString *const expression = @"^\\d{3}[-]\\d{2}[-]\\d{4}$"; // insert yours
    NSError *error = nil;

    NSRegularExpression * const regExpr = 
    [NSRegularExpression regularExpressionWithPattern:expression 
                                options:NSRegularExpressionCaseInsensitive 
                                error:&error];

    NSTextCheckingResult * const matchResult = [regExpr firstMatchInString:string
                              options:0 range:NSMakeRange(0, [string length])];

   return matchResult ? YES : NO; 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to validate string against regular expression string

How to match a string against an existing regular expression?

How to check if the string is a regular expression or not

How to check sequence in string using regular expression

How to check if the input string is a valid Regular expression?

How to Split String With Regular Expression ios objectivec

iOS Swift Regular expression, how to match a string?

Regular Expression Check: return boolean and answers

How to affect to a boolean value the result of a boolean? expression

How to match a regular expression against another regular expression in javascript?

Regular expression to check a string starts with

How to match a list against a regular expression in Groovy

How to check if entire string matches regular expression in Javascript?

How to use a regular expression to check for duplicate characters in a string?

How to check if a python string is a valid Bengali word using regular expression?

How to use regular expression result into another expression

How to split this string and get the number separately in ios regular expression

How to match a string against a list of regex expression

regular expression validator to check if string has number

Check string for regular expression and select a group

Using regular expression to check for pangram in a string error

Check for comma delimited string with regular expression

Regular expression to check if a string match certain pattern

PHP Regular Expression to check Alphameric String

Other Ways to Check if String is a Regular Expression

Regular expression to check if string contains only zeros

Check if a String matches specific regular expression

python - Regular expression to check if string matches pattern

How to check if string not equals a Boolean