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!@$&#]*$
this regular expression following:
(?=.*[a-z])
require string contain @ least 1a-z
character(?=.*[0-9])
require string contain @ least 10-9
character[a-z0-9!@$&#]*$
allow string made ofa-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
Post a Comment