Regular expression

Regular expression

I used to judge whether the string in the UITextField is in digital form, which is foolish to judge the content inside. The operation is as follows:

//Determine whether it is a number if ([self isPureInt:self.lengthTF.text] || [self isPureFloat:self.lengthTF.text]) { //Either an integer or a floating point number [self setShowInfomaton]; }else{ [SVProgressHUD showErrorWithStatus:NSLocaString(@"Please enter the number format")]; } //Determine whether it is an integer -(BOOL)isPureInt:(NSString*)string{ NSScanner* scan = [NSScanner scannerWithString:string]; int val; return [scan scanInt:&val] && [scan isAtEnd]; } //Determine whether it is a floating point -(BOOL)isPureFloat:(NSString*)string{ NSScanner* scan = [NSScanner scannerWithString:string]; float val; return[scan scanFloat:&val] && [scan isAtEnd]; } Copy code

Using regular expressions, the operation is easier. as follows:

//Verify the number static NSString * const Regex_Number = @"^((^0\\.\\d{0,2}$)|(^[1-9]+\\d*\\.\\d{0,2} $)|(^[1-9]\\d*$)|(^0$))?$"; NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Regex_Number]; if ([predicate evaluateWithObject:self.lengthTF.text]) { [self setShowInfomaton]; }else{ [SVProgressHUD showErrorWithStatus:NSLocaString(@"Please enter the number format")]; } Copy code

Of course, there are many types that support judgment and verification!

Regular expression

It is a logical formula for string manipulation. Regular expressions can detect whether a given string conforms to the logic we define, and can also get the specific part we want from the string.

  • Look at an example of filtering (determining whether it is) a pure integer number

    -(BOOL)validateNumber:(NSString *)textString { NSString* numberStr = @"^[0-9]+$"; NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numberStr]; return [numberPre evaluateWithObject:textString]; } Copy code

    Verification and console output:

    NSString * compStr = @"iphone7"; BOOL isTotallyNumber = [self validateNumber:compStr]; NSLog(@"%d",isTotallyNumber); //Method of outputting BOOL value NSLog(@"%@",isTotallyNumber?@"YES":@"NO"); Copy code

    NSString * compStr = @"01234447"; BOOL isTotallyNumber = [self validateNumber:compStr]; NSLog(@"%d",isTotallyNumber); //Method of outputting BOOL value NSLog(@"%@",isTotallyNumber?@"YES":@"NO"); Copy code

    Among them, @"^[0-9]+$" is a regular expression, which means that the string can only contain ">=1" numbers of "0-9".

syntax:

First of all, the special symbols'^' and'$'. Their function is to point out the beginning and end of a string respectively. "^one": Represents all strings starting with "one" ("one cat", "one123", ); similar to:

-(BOOL)hasPrefix:(NSString *)aString; Copy code

"A dog$": represents the string ending with "a dog" ("it is a dog", ); similar to:

-(BOOL)hasSuffix:(NSString *)aString; Copy code

"^apple$": Represents a string of "apple" at the beginning and end, this is the only one~; "banana": Represents any string containing "banana". New method similar to iOS8:

-(BOOL)containsString:(NSString *)aString, used for searching substrings. Copy code

In addition, the three symbols of'*','+' and'?' indicate the number of repeated occurrences of one or N characters. They respectively mean "no or more" ([0,+ ] rounded), "one or more" ([1,+ ] rounded), "no or once" ([0,1] rounded ).

Here are a few examples: "ab *": indicates a string followed by a zero or several months b ( "a", "ab ", "abbb", ......); "ab +": represents a character A string has an a followed by at least one b or more ("ab", "abbb",...); "ab?": indicates that a string has an a followed by zero or one b ("a", " ab"); "a?b+$": Indicates that there are * zero or one a* followed by * one or several b* at the end of the string ("b", "ab", "bb", "abb", ...).

Which can brace bracketed ({}), represents a specific range of repetition. For example: "ab{4}": Indicates that a string has an a followed by 4 bs ("abbbb"); "ab{1,}": Indicates that a string has an a followed by at least 1 b ("ab" ,"Abb","abbb",......); "ab{3,4}": Indicates that a string has an a followed by 3 to 4 bs ("abbb", "abbbb"). Then, "** * " can be represented by {0,} , " + " can be represented by {1,} , and " ? "> can be represented by {0, 1}**. Note : There can be no lower limit, but no upper limit! For example, "ab{,5}" is wrong.

"|" Means "or" operation: "a|b": means "a" or "b" in a string ; "(a|bcd)ef": means "aef" or "bcdef"; "(a |b)*c": Represents a string of "a" and "b" mixed with a "c";

The square brackets "[ ]" indicate that among the many characters in the brackets, select 1-N grammatical characters in the brackets as the result .

E.g:

"[Ab]": indicates that a string has an "a" or "b" (equivalent to "a|b"); "[ad]": indicates that a string contains a lowercase'a' to 'd' in One of (equivalent to "a|b|c|d" or "[abcd]"); "^[a-zA-Z]": represents a string beginning with a letter ; "[0-9]a" : Indicates that there is a number before a ; "[a-zA-Z0-9]$": Indicates that a character string ends with a letter or number .

"." matches any single character except "\r\n":

"A.[az]": Indicates that a string has an "a" followed by an arbitrary character and a lowercase letter;

"^.{5}$": Represents any string of length 5;

"\Num" where num is a positive integer . Represent "/num" before the characters appear the same in number . For example: "(.)\1": means two consecutive identical characters. "10{1,2}": Indicates that the number 1 is followed by 1 or 2 0s ("10","100"). "0{3,}" means that the number is at least 3 consecutive 0s ("000", "0000", ).

In the square brackets with ** '^' denotes undesired characters**, and'^' should be the first place in the square brackets. "@[^a-zA-Z]4@" means that there should be no letters in the two "@").

Also commonly used: "\d" matches a digit character. Equivalent to [0-9]. "\D" matches a non-digit character. Equivalent to [^0-9]. "\W" matches any word character including underscores. Equivalent to "[A-Za-z0-9_]". "\W" matches any non-word character. Equivalent to "[^A-Za-z0-9_]".

When writing regular expressions in iOS, when you encounter an escape character, add an extra "\", for example:

All numeric characters: @ ^\d+$

Regular expressions in iOS

[1]. Regular expressions are used in conjunction with NSPredicate (predicate class).
//Define the regular expression as a static string //phone number static NSString * const Regex_PhoneNum = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$" ; //mailbox static NSString * const Regex_Email = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4 }"; //identity number static NSString * const Regex_IDCardNumber = @"^(\\d{14}|\\d{17})(\\d|[xX])$"; //digital static NSString * const Regex_Number = @"^((^0\\.\\d{0,2}$)|(^[1-9]+\\d*\\.\\d{0,2} $)|(^[1-9]\\d*$)|(^0$))?$"; //number plate static NSString * const Regex_CarNumber = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_/u4e00-\u9fa5]$"; //Model static NSString * const Regex_CarType = @"^[\u4E00-\u9FFF]+$"; //username static NSString * const Regex_UserName = @"^[A-Za-z0-9]{6,20}+$"; //password static NSString * const Regex_Code = @"^[a-zA-Z0-9]{6,20}+$"; //nickname static NSString * const Regex_nickName = @"^[\u4e00-\u9fa5]{4,8}$"; Copy code

In order to make the demonstration simple, use main.storyboard to drag the control directly.

-(void)viewDidLoad { [super viewDidLoad]; //put in the global array _predicateArr = @[Regex_PhoneNum,Regex_Email,Regex_IDCardNumber,Regex_Number,Regex_CarNumber,Regex_CarNumber,Regex_CarType,Regex_UserName,Regex_Code,Regex_nickName]; } Copy code

-(IBAction)buttonTouch:(UIButton *)sender { //predicate NSPredicate NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[0]]; if ([predicate evaluateWithObject:self.inputTF.text]) { NSLog(@"This is a mobile phone number"); self.textLB.text = @"This is a mobile phone number"; }else{ NSLog(@"Not a mobile phone number"); self.textLB.text = @"Not a mobile phone number"; } //According to your own needs, just replace the corresponding regular expression in the array to judge the operation. //NSPredicate * predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[1]];//predicate //if ([predicate1 evaluateWithObject:self.inputTF.text]) { //NSLog(@"This is an Email"); //self.textLB.text = @"This is an Email"; //return; //}else{ //NSLog(@"Not Email"); //self.textLB.text = @"Not Email"; //} //NSPredicate * predicate2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[2]];//predicate //if ([predicate2 evaluateWithObject:self.inputTF.text]) { //NSLog(@"This is the ID number"); //self.textLB.text = @"This is the ID number"; //}else{ //NSLog(@"Not an ID number"); //self.textLB.text = @"Not an ID number"; //} // //NSPredicate * predicate3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[3]];//predicate //if ([predicate3 evaluateWithObject:self.inputTF.text]) { //NSLog(@"This is a number"); //self.textLB.text = @"This is a number"; //}else{ //NSLog(@"Not a number"); //self.textLB.text = @"Not a number"; //} // // //NSPredicate * predicate4 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[4]];//predicate //if ([predicate4 evaluateWithObject:self.inputTF.text]) { //NSLog(@"This is the license plate number"); //self.textLB.text = @"This is the license plate number"; //}else{ //NSLog(@"Not a license plate number"); //self.textLB.text = @"Not a license plate number"; //} // //NSPredicate * predicate5 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[5]];//predicate //if ([predicate5 evaluateWithObject:self.inputTF.text]) { //NSLog(@"This is the model"); //self.textLB.text = @"This is the model"; //}else{ //NSLog(@"Not a model"); //self.textLB.text = @"Not a model"; //} // //NSPredicate * predicate6 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[6]];//predicate //if ([predicate6 evaluateWithObject:self.inputTF.text]) { //NSLog(@"This is the username"); //self.textLB.text = @"This is the username"; //}else{ //NSLog(@"Not a username"); //self.textLB.text = @"Not a username"; //} // //NSPredicate * predicate7 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[7]];//predicate //if ([predicate7 evaluateWithObject:self.inputTF.text]) { //NSLog(@"This is the password"); //self.textLB.text = @"This is the password"; //}else{ //NSLog(@"Not a password"); //self.textLB.text = @"Not a password"; //} // //NSPredicate * predicate8 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",_predicateArr[8]];//predicate //if ([predicate8 evaluateWithObject:self.inputTF.text]) { //NSLog(@"This is a nickname"); //self.textLB.text = @"This is a nickname"; //}else{ //NSLog(@"Not a nickname"); //self.textLB.text = @"Not a nickname"; //} } Copy code

effect:

Using regular expressions will not be like when using strings to determine "Is it a number?", it may ignore the fact that the value is a decimal. Regular expressions can ensure that the format is completely correct! ! ! We only need to deal with it where we need to judge.

[2]. Use NSString method

The " [rangeOfString: options: ]; " method will return a NSRange to receive the matched range. If there is no match, it will return a NSIntegerMax (maximum), namely: NSNotFound. #define NSIntegerMax LONG_MAX//The maximum value of the signed long integer, so it can be used to determine whether the content entered by the user meets the rules.

NSString *searchText = @"rangeOfString62323"; NSRange range = [searchText rangeOfString:@"^[0-9]+$" options:NSRegularExpressionSearch]; //"^[0-9]+$": Indicates that the string can only contain at least one number from 0-9 if (range.location != NSNotFound) { NSLog(@"range %@", [searchText substringWithRange:range]); NSLog(@"Location:%lu Length:%lu",(unsigned long)range.location,range.length); } Copy code

Effect: The value of " range.location " is the maximum value. If the condition of the if statement is not satisfied, the judgment statement is not entered!

Put on top

NSString *searchText = @"rangeOfString62323"; Copy code

Change the value of the searchText string to numeric form:

NSString *searchText = @"13112345678"; Copy code

The console prints the result:

[3]. Regular expression class (NSRegularExpression)

[3.1] Get a single string

//Use regular expressions to intercept strings NSString * compStr = @"http://www.jianshu.com/users/0c0805f8de58/latest_articles"; NSError * error; //Create an NSRegularExpression object and specify a regular expression for it NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"[^an]*\\m" options:0 error:&error]; //@"[^X1 start position[not included])]*\\X2 end position[include])" if (!error) { //Get the range of a specific string NSTextCheckingResult * match = [regex firstMatchInString:compStr options:0 range:NSMakeRange(0, [compStr length])]; if (match) {//Can match //intercept a specific string NSString * result = [compStr substringWithRange:match.range]; NSLog(@"%@",result);//Result: "shu.com" } } else { //If there is an error, print the error NSLog(@"error-%@", error); } Copy code

The console prints the result:

What is retrieved from the string is a range string beginning with "an" and ending with "m", and the end of the finally retrieved string includes "m", so the final output result is "shu.com".

[3.2] Obtaining multiple strings When there are multiple characters that meet specific rules in a string, we can obtain the characters that meet specific rules respectively:

NSString * regex = @"\\_\\d*\\."; //Start with "_", end with "." (both included) and take the integer part NSString * compStr = @"_233423._dsakl3223.122_2abcl23.dfd_3242.34.322_"; NSError * error; NSRegularExpression * regularEx = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:&error]; //Match the compStr string NSArray * matchStr_Arr = [regularEx matchesInString:compStr options:0 range:NSMakeRange(0, compStr.length)]; //traverse every record matched for (NSTextCheckingResult * match in matchStr_Arr) { NSRange range = [match range]; NSString * elementStr = [compStr substringWithRange:range]; NSLog(@"%@", elementStr); } Copy code

Obtain integer characters starting with "-" and ending with "." from the specified string, because it may contain multiple strings that meet its rules. Therefore, they need to be traversed.

Regular expression summary

The following regular expressions are from Baidu Encyclopedia:

1. Verify the user name and password: "^[a-zA-Z]\w{5,15}$" 2. Verify the phone number: ("^(\\d{3,4}-)\\d{7,8}$") eg:021-68686868 0511-6868686; 3. Verify the phone number: "^1[3|4|5|7|8][0-9]\\d{8}$"; 4. Verify the ID number (15 or 18 digits): "\\d{14}[[0-9],0-9xX]"; 5. Verify Email address: ("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\.\\w+([-.]\\w+ )*$ ); 6. You can only enter a string consisting of numbers and 26 English letters: ("^[A-Za-z0-9]+$"); 7. Integer or decimal: ^[0-9]+([.]{0,1}[0-9]+){0,1}$ 8. Only numbers can be input: "^[0-9]*$". 9. Only n-digit numbers can be input: "^\\d{n}$". 10. Only enter a number with at least n digits: "^\\d{n,}$". 11. Only m~n digits can be input: "^\\d{m,n}$". 12. Only zeros and numbers starting with non-zeros can be input: "^(0|[1-9][0-9]*)$". 13. Only positive real numbers with two decimal places can be input: "^[0-9]+(.[0-9]{2})?$". 14. Only positive real numbers with 1~3 decimal places can be input: "^[0-9]+(\.[0-9]{1,3})?$". 15. Only non-zero positive integers can be input: "^\+?[1-9][0-9]*$". 16. Only non-zero negative integers can be entered: "^\-[1-9][]0-9 *$. 17. Only characters with a length of 3: "^.{3}$" can be entered. 18. Only a string consisting of 26 English letters can be input: "^[A-Za-z]+$". 19. Only a string consisting of 26 uppercase English letters can be input: "^[AZ]+$". 20. You can only enter a string consisting of 26 lowercase English letters: "^[az]+$". 21. Verify whether there are characters such as ^%&',;=?$\": "[^%&',;=?$\x22]+". 22. Only Chinese characters can be input: "^[\u4e00-\u9fa5]{0,}$". 23. Verification URL: "^http://([\\w-]+\.)+[\\w-]+(/[\\w-./?%&=]*)?$". 24. Verify the 12 months of a year: "^(0?[1-9]|1[0-2])$" The correct format is: "01"~"09" and "10"~"12". 25. Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$" The correct format is; "01" " 09", "10"~"29" and "30"~"31". 26. Get date regular expression:\\d{4}[year|\-|\.]\\d{\1-\12}[month|\-|\.]\\d{\1-/31) day? Comment: Can be used to match most year, month, and day information. 27. Match double-byte characters (including Chinese characters): [^\x00-\xff] Comment: Can be used to calculate the length of the string (a double-byte character counts as 2, ASCII character counts as 1) 28. Regular expression matching blank lines:/n\s*\r Comment: Can be used to delete blank lines 29. Regular expression matching HTML tags: <(\S*?)[^>]*>.*?</>|<.*?/> Commentary: The version circulating on the Internet is too bad. The above one can only match part of it, and it is still helpless for complex nested tags. 30. Regular expression matching the first and last blank characters: ^\s*|\s*$ Comment: It can be used to delete the blank characters at the beginning and end of the line (including spaces, tabs, form feeds, etc.), a very useful expression 31. Regular expression to match the URL: [a-zA-z]+://[^\s]* Commentary: The functions of the versions circulating on the Internet are very limited, and the above one can basically meet the needs 32. Whether the matching account number is legal (beginning with a letter, allowing 5-16 bytes, allowing alphanumeric underscores): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ Comment: very useful for form validation 33. Match Tencent QQ number: [1-9][0-9]\{4,\} Commentary: Tencent QQ account starts from 10 000 34. Match Chinese postal code: [1-9]\\d{5}(?!\d) Commentary: Chinese postal code is 6 digits 35. Match ip address: ((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?). Copy code

Most of the content has been used by myself, and some have been used, so I won t elaborate on them one by one~~~~

If there are any errors, please point out!

( 2016.12.07 )

goyohol's essay