iOS develops regular operations in detail

  • 2020-12-21 18:11:16
  • OfStack

iOS develops regular operations

1. Meaning of regular operation

regular expression describes a pattern of string matching, which can be used to check whether a string contains a certain seed string, replace the matching substring, or extract a substring that meets a certain condition from a string, etc. In iOS, we mostly use to check whether a string conforms to rules

2. Regular regular expressions (if you want to write your own regular expressions that fit your needs, you only need to know the symbolic meaning in 4)

(1). The user name

"^[a-zA-Z][a-zA-Z0-9_]{5,15}$" (composed of 6-16 alphanumeric underscores, the first digit cannot be a number or underscore)
[ES22en-ES23en-ES24en] indicates that the first digit is the letter;
[ES26en-ES27en-Z0-9] indicates that characters are alphanumeric or slide lines;
{5,15} indicates that there are 5 to 15 characters that match [a-ES31en-Z0-9]

(2). The password

"^ (& # 63; ! Aiaa [0-9] {0}) [\ \ S] 6, 24} {$" (6-24, not for pure digital, can't contain Spaces)
(& # 63; ! [0-9]{0,24}$) means that the first 24 digits cannot be all numbers
\S represents any non-whitespace character (no whitespace entered by space, enter,tab, etc.)
[\ S]{6,24} represents a 6 - to 24-bit character that is not a space

(3). Mobile phone number

"^1[3|, 4|, 5|, 7|8][0-9]{9}$"
1 means the first bit is 1
[3|,4 |,5 |,7 |, 8] means the second digit is 3,4,5,6,7, or 8
[0-9]{9} represents any 9-digit number

(4).email

"^[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9_-]+.com(.cn)? $" (format username @mailbox site.com or user. com.cn)

[ES75en-ES76en-Z0-9_ -]+ represents 1 or more alphanumeric underscores and connectors
(.[ES79en-ES80en-Z0-9_ -]+)* indicates 0 or more points by 1. A string consisting of one or more alphanumeric underscore connectors

3. Application of regularization in iOS

Check that the string meets the requirements


/**
 *  Check that the string matches the regular expression is 
 *
 * @param string  Strings that need to be checked 
 * @param regex  The regular expression is 
 *
 * @return  In return YES  Does not return NO
 */
- (BOOL) predicateString:(NSString *)string WithRegularExpreess:(NSString *)regex {
  // 1 , splicing predicate 
  NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
  // 2 , match string 
  BOOL result = [predicateRe evaluateWithObject:string];
  return result;
}

 Such as : Check if the string matches  (6-24 position , It can't be a pure number , Cannot contain Spaces )  The rules of 

 NSString *regulerExp = @"^(?![0-9]{0,24}$)[\\S]{6,24}$";
 BOOL result = [self predicateString:_textField.text WithRegularExpreess:regulerExp];
  

4. Common symbols are meanings (after understanding the meanings of the following symbols, regular operations like 1 can be written)

Used at the beginning of an expression to indicate that a regular expression is the beginning
$represents the end of the string


The expression in [] is limited to 1 character
For example: ^[ES98en-ES99en]: limited to lowercase letters [ES100en-ES101en]: limited to uppercase letters [0-9]: limited to numbers
() qualifies 1 substring
For example, ^([a-ES105en]{4})([0-9]{3}) represents a string consisting of four lowercase letters followed by three numbers, i.e. abcd332 adfd453, etc


{m} m qualified characters
For example: ^[ES114en-ES115en]{3} represents a string consisting of three lowercase letters, i.e. add bcd ade, etc
{m,} greater than or equal to m qualifying characters
For example: ^[ES123en-ES124en]{3,} represents a string consisting of more than three lowercase letters, namely, acd,bcd, adsf, sadfasdfasdf, etc
{m,n} m to n qualified characters
For example: ^[a-ES136en]{3,5} represents a string consisting of 3 to 5 lowercase letters, i.e. adb adsb asdfd, etc
* represents 0 or more qualified characters
For example :^[a-ES143en]* represents a string consisting of 0 or more lowercase letters
The & # 63; Represents 0 or 1 qualified characters
+ represents one or more qualified characters
Note: Single characters without qualifier tags, where m,n are non-negative integers,n > m


[^] ^ stands for obligatory meaning in []
For example, ^[^ ES156en-ES157en] represents a character except one lowercase letter
| or
For example, ^[a-z]|[0-9] represents a lowercase letter or number

The & # 63; = Included in parentheses to check for characters that conform to the rules in parentheses, such as: (? = [0-9]{0-7}) means that the first seven digits are all numbers
The & # 63; ! Include in parentheses to indicate that you are checking for characters that do not conform to the rules in parentheses, such as: (? ! [0-9]{0-7}) means that the first seven digits are not all numbers

Through this article, I hope you learn IOS regular operation master, thank you for your support to this site!


Related articles: