ZIGOoL ZIGOoL - 6 months ago 13
iOS Question

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

In the below method, I want to check a

UITextField
Text if:
it contains one or more English words, and one or more numbers and 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

Answer

Description

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

Regular expression visualization

This regular expression will do the following:

  • (?=.*[a-z]) Require the string to contain at least one a-z character
  • (?=.*[0-9]) Require the string to contain at least one 0-9 character
  • [a-z0-9!@$&#]*$ Allow the string to be made up of only a-z characters, 0-9 characters, and !@$&# symbols

Example

Live Demo

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

Explanation

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