1) mobile phone number check 2) password check 3) iP address check 4) QQ number check; Please draw inferences when using!

Regular expressions can be used to: (1) verify that a string matches specified characteristics, such as a valid email address. (2) used to find the string, from a long text to find a string in line with the specified characteristics, more flexible than looking for a fixed string. (3) Is used for substitution, which is more powerful than normal substitution.

Of course, here are our background rules, these regular expression rules are designed according to your product greatly needs, without further ado, directly on the code:


#import "NSString+Extension.h"@implementation NSString (Extension) - (BOOL)match:(NSString *)pattern { // 1. NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil]; NSArray *results = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];returnresults.count > 0; } // Add the following @end to the verificationCopy the code
  1. Mobile phone number verification
- (BOOL)isPhoneNumber {// 1. All digits // 2.11 bits // 3. Background checked with 13\15\18\17return [self match:@"^1\\d{10}$"];
//    return [self match:@"^(13\\d|14[57]|15[^4,\\D]|17[678]|18\\d)\\d{8}$|^170[059]\\d{7}$"];
}
Copy the code
  1. Password checking
- (BOOL)isPSW {// The value must start with a letter and contain 6 to 18 characters, digits, and underscores (_)return [self match:@"^ \ \ w [a zA - Z] {5} in 2 $"]; // Contains 6 to 16 characters, digits, and underscores (_)return [self match:@"^ [0-9 a zA - Z] \ \ w {5, 15} $"];
}
Copy the code
  1. IP address verification
- (BOOL)isIPAddress {// 1-3 digits: 0-255 // 1-3 digits.1-3 digits.1-3 digits.1-3 digits.1-3 digitsreturn [self match:@"^ \ \ d {1, 3} \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} $"];
}
Copy the code
  1. QQ number check
- (BOOL)isQQ {// 1. Cannot start with 0 // 2. All numbers // 3.5-11 digitsreturn [self match:@"^ 1-9] [\ \ d {4, 10} $"];
}

Copy the code

Where you need validation processing, just call the method exposed above in.h, for example:

- (void)loginPress {
   
    if(! [self.mobileField.text isPhoneNumber]) { [MBProgressHUD showError:@"Mobile phone number input does not comply with the rules"];
    } else if(! [self.pswField.text isPSW]) { [MBProgressHUD showError:@"Password does not meet requirements"];
    } else{// here is calling the login interface related processing}}Copy the code

To learn more about expression rules, please refer to:

Ordinary characters Letters, digits, Chinese characters, underscores (_), and punctuation marks that are not defined in subsequent chapters are all “ordinary characters”. A normal character in an expression that matches a string with the same character.

###1.2 Simple escape characters Some characters are inconvenient to write, use the method of adding “” in front. These characters are already familiar to us.

\r, \n stands for carriage return and newline character \t TAB character \ stands for “” itself

There are other punctuation marks that are of special use in later chapters, where “” is used to represent the mark itself. For example, “^” and “$” have special meanings. If you want to match the “^” and “$” characters in the string, the expression needs to be written as “^” and “$”.

^ Match ^ symbol itself $match $symbol itself. Match decimal point (.) itself

A number of representations in regular expressions that can match any of the ‘multiple characters’. For example, the expression “\d” can match any number. Any character can be matched, but only one character, not multiple characters. It’s like playing poker, where Kings can replace any card, but only one.

\d Any digit, any of 0 to 9 \w Any letter, digit, or underscore (_), that is, any of A to Z, A to Z, 0 to 9,_ \s any of blank characters, such as Spaces, tabs, and page feed characters. The decimal point can match any character except newline (\n)

Expressions that can match ‘multiple characters’ contain a list of characters using square brackets [] that can match any one of them. If [^] contains a series of characters, any other character can be matched. Similarly, you can match any one of them, but only one, not multiple.

[ab5 @] match “a” or “b” or “5” or “@” [ABC] ^ match “a”, “b”, “c” outside of any one character (f-k) match between “f” and “k” of any one letter [^ a – F0-3] match “a” to “f”, “0” and “3” Any character other than

Expressions described in the previous section can match only once, whether they match only one character or any one of many characters. If you use an expression plus a special symbol that decorates the number of matches, you can repeat matches without having to write the expression twice.

This is done by placing the "number modifier" after the "expression being modified". For example, "[BCD][BCD]" can be written as "[BCD]{2}".Copy the code

The {n} expression is repeated n times, for example: “\w{2}” equals “\w\w”; A {5}” is equivalent to “aaaaa” {m,n} expressions repeated at least m times and at most n times, e.g. “ba{1,3}” can match “ba” or “baa” or “baaa” {m,} expressions repeated at least m times, e.g. “\w\d{2,}” can match “a12″,”_456”,”M12344″… ? Match the expression 0 or 1 times, equivalent to {0,1}, for example: [url=http://www.regexlab.com/zh/workshop.htm?pat=a [CD] % 3 f & TXT = a, c, d, ac, AD] a [CD]? “” can match” a “, “ac”, “AD” [/ url] + expression at least, Equivalent to {1,}, for example: “a+b” matches “ab”,”aab”,”aaab”… * * * * * or expressions don’t occur at any time, equivalent to {0}, for example: “^ * b” matches “b”, “^ ^ ^” b…

Some symbols represent abstract special meanings in expressions:

^ Matches the beginning of the string, does not match any character $matches the end of the string, does not match any character \b matches a word boundary, that is, the position between a word and a space, does not match any character

Symbols can affect the expression of the relationship between the internal expression: | between left and right sides expression “or” relationship, matching the left or right () (1). When the number of matches is modified, the expression in parentheses can be modified as a whole (2). When taking a match, the content matched by the expression in parentheses can be obtained separately

When using special symbols to denote the number of matches, there are several ways to express the same expression to match different numbers of matches. For example: “{m,n}”, “{m,}”, “?” , “*”, “+”, the number of matches depends on the string being matched. This kind of repeated match indefinite number of expressions in the process of matching, always as many matches as possible. For example, for the text “DXXXDXXXD”, here is an example:

(d) (\ w +) “\ w +” will match any characters following the first “d” “XXXDXXXD” (d) (\ w +) (d) “\ w +” will match the first “d” and finally a “d” all characters between “XXXDXXX”. Although “\w+” can also match the last “d”, in order for the whole expression to match, “\w+” can “yield” the last “d” that it would otherwise match

Thus, “\w+” always matches as many characters as it can when matching. In the second example, it doesn’t match the last “D”, but that’s for the whole expression to match. Similarly, expressions with “*” and “{m,n}” match as many as possible, with “?” The expression “to match” is also “to match” whenever possible. This matching principle is called the “greed” pattern. Non-greedy mode:

A special symbol for the number of matches followed by a “?” “, can make the number of variable expressions match as little as possible, so that can match the expression does not match, as much as possible “do not match”. This matching principle is called the “non-greedy” model, also known as the “forced” model. If there are fewer matches, the whole expression will fail to match. Similar to greedy mode, the non-greedy mode will match more matches at a minimum to make the whole expression match successfully. For example, for the text “DXXXDXXXD” :

(d)(\w+?) “\w+?” Matches as few characters after the first “d” as possible, resulting in: “\w+?” Only one “x” (d)(\w+?) matches. (d) In order for the entire expression to match, “\w+?” You have to match “XXX” in order for the following “d” to match, so that the entire expression matches. Thus, the result is: “\w+?” Matching “XXX”

###2.2 Backquotes \1, \2… When an expression matches, the expression engine records the string matched by the expression contained in the parentheses “()”. The string matched by the expression contained in the parentheses can be retrieved separately. This point has been shown many times in the previous examples. In practical applications, when looking up with some kind of boundary, and what you want to get does not contain a boundary, you must specify the desired range with parentheses. For example, “(.*?) “.

In fact, “the string matched by the expression contained in the parentheses” can be used not only after the match, but also during the match. The following part of the expression can refer to the string already matched by the submatch in parentheses. The way to quote it is “add a number.” \1″ refers to the string matched in the first pair of brackets, “\2” refers to the string matched in the second pair of brackets… Similarly, if a pair of parentheses contains another pair of parentheses, the outer parentheses are sorted first. In other words, whichever pair comes before the opening parenthesis “(“, that pair is sorted first.

###2.3 Pre-search, do not match; In the previous chapter, I talked about some special symbols that represent abstract meanings: “^”, “$”, “\b”. They all have one thing in common: they don’t match any characters themselves, just a condition for “both ends of the string” or “the space between characters.” With this concept in mind, this section goes on to introduce another, more flexible way of saying “both ends” or “gaps” with conditions attached. Forward presearch: “(? (= XXXXX) “, “? ! xxxxx)”

Format: “(? = XXXXX)”, in the matched string, the condition that it is in the “gap” or “two ends” is that the right side of the gap must match the XXXXX part of the expression. Because it is only there as an additional condition on the gap, it does not affect subsequent expressions to actually match the characters after the gap. This is similar to “\b”, which itself does not match any characters. \b” is only used to select the characters before and after the gap. It does not affect the following expression to match.

The concept of these two formats is similar to that of forward pre-search. Reverse pre-search requires that the conditions are: the “left side” of the gap. The two formats require that the specified expression must be matched and must not be matched, instead of judging the right side. Like “forward pre-search”, they are an additional condition to the gap in which they are located and do not match any characters themselves.

There are a few rules that are common between regular expression engines and are not mentioned in the previous tutorial.

In ###3.1 expressions, “\xXX” and “\uXXXX” can be used to represent a character (“X” represents a hexadecimal number).

\xXX Characters numbered from 0 to 255, for example: Spaces can be represented by “\x20” \uXXXX Any character can be represented by “\u” followed by its numbered 4-digit hexadecimal number, for example: “\u4E2D”

###3.2 While the expressions “\s”, “\d”, “\w”, “\b” denote special meanings, the corresponding capital letters denote opposite meanings

\S matches all non-white space characters (“\ S “matches all white space characters) \D matches all non-numeric characters \W matches all characters other than letters, digits, and underscores \B matches non-word boundaries, that is, the character gap between the left and right sides of the “\ W” range or the left and right sides of the “\ W “range

###3.3 has special meaning in expressions that require the addition of “” to match the sum of characters of the character itself

^ Matches the start of the input string. To match the “^” character itself, use “^” $to match the end of the input string. To match the “$” character itself, mark the beginning and end of a subexpression with “$” (). To match the parentheses, use “(” and “)” [] to customize an expression that matches’ multiple characters’. To match brackets, use the symbol “[” and “]” {} to decorate the number of matches. To match braces, use “{” and “}”. Match any character other than newline (\n). To match the decimal point itself, use “.”? The number of modification matches is 0 or 1. To match the “?” For the character itself, use “?” + Modifier matches at least once. To match the “+” character itself, use the “+” ** * ** modifier to match 0 or any number of times. To match the “*” character itself, please use the “*” | between left and right sides expression “or” relationship. Matching “|” itself, please use the “|”

###3.4 Subexpression inside parentheses “()”, if you want the match result not to be recorded for later use, you can use “(? Format: XXXXX)”

If you think it is useful, point a like to me hey hey ~