objective c - Regular Expressions in ios(a word a letter and special characters) -


in below method, want check uitextfield text if: contains 1 or more english words, , 1 or more numbers , let optionally contains special characters(!@$&#) return true, else return false.

#define pattern   @"^[a-za-z0-9\\u0021\\u0040\\u0023\\u0024\\u0026]{1}*$"  - (bool)stringhascorrectformat:(nsstring *)str {        if ([str componentsseparatedbystring:space].count > 1)          return no;       nsstring *regularpattern = empty_string;       regularpattern = [regularpattern stringbyappendingstring:pattern]       nsregularexpression *regex = [nsregularexpression                               regularexpressionwithpattern:regularpattern                               options:nsregularexpressioncaseinsensitive                               error:nil];          nstextcheckingresult *match = [regex                                firstmatchinstring:str                                options:0                                range:nsmakerange(0, [str length])];        if (match != nil) {          return yes;      }       return no; } 

thanks

description

^(?=.*[a-z])(?=.*[0-9])[a-z0-9!@$&#]*$ 

regular expression visualization

this regular expression following:

  • (?=.*[a-z]) require string contain @ least 1 a-z character
  • (?=.*[0-9]) require string contain @ least 1 0-9 character
  • [a-z0-9!@$&#]*$ allow string made of a-z characters, 0-9 characters, , !@$&# symbols

example

live demo

https://regex101.com/r/mc3kl3/1

explanation

node                     explanation ----------------------------------------------------------------------   ^                        beginning of "line" ----------------------------------------------------------------------   (?=                      ahead see if there is: ----------------------------------------------------------------------     .*                       character except \n (0 or more times                              (matching amount possible)) ----------------------------------------------------------------------     [a-z]                    character of: 'a' 'z' ----------------------------------------------------------------------   )                        end of look-ahead ----------------------------------------------------------------------   (?=                      ahead see if there is: ----------------------------------------------------------------------     .*                       character except \n (0 or more times                              (matching amount possible)) ----------------------------------------------------------------------     [0-9]                    character of: '0' '9' ----------------------------------------------------------------------   )                        end of look-ahead ----------------------------------------------------------------------   [a-z0-9!&#]*             character of: 'a' 'z', '0' '9',                            '!', '&', '#' (0 or more times (matching                            amount possible)) ----------------------------------------------------------------------   $                        before optional \n, , end of                            "line" ---------------------------------------------------------------------- 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -