Regular expressions for iOS (i)

Source: Internet
Author: User
Tags uppercase letter

It's a good thing to study regular expressions these days to find regular expressions that can easily match the amount of the transaction at the time of payment, the introductory content, and the following:

@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet Uitextfield*Texttf, @property (weak, nonatomic) Iboutlet UILabel*label; @property (Weak, nonatomic) Iboutlet UIButton*testbtn;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //1. The function of regular expressions to easily and quickly match to a specific string, simplifying the amount of code//Example Analysis /* ^. {6,18}$ expression means matching strings of any length from 6 to 18 bits where ^ and $ represent the special symbol, which represents the beginning of the matching string, which represents the end of the matching string, where {n} indicates that the matching character repeats n times, {n,} matches the character repeatedly n or more times, {n,m} Matches the number of times between N and M, whereas the. symbol represents any character (except newline characters); */ /*For exact matching, for example, a match to a specific number or letter can be represented by a special symbol, where \d represents any single number, \w represents any single letter or number, or directly using any number 0-9 to represent a specific number. and a special symbol? indicates that the previous character is 0 or 1. So the shape as ^0\\d{2}\-?\\d{8}$ means that the match first character is 0 followed by two digits, followed by a "-" symbol or not, followed by 8 numbers; One thing to note is that the expressions shown above \d These special symbols in our code, which is Because \ itself is an escape symbol, in order to ensure that the expression can be properly matched, we have to give \d an escape, so it becomes \ \. Basically all symbolic characters need to be escaped.*/ /*the split of a regular expression can be divided into two parts, a value expression, and a modified expression. The so-called value expression means that the symbol represents a value, just like "\d" means a number, "." Represents any non-line break character. A decorated expression is used to modify a value to achieve a condition, such as {2}, which indicates that the previous value repeats two times, and "*" indicates that the previous value was repeated 0 or more times. According to this method, the ^.*[a-z]+.*[a-z]+.*$ can be split into: ^$,. *, [a-z]+, [a-z]+]. Note: * Indicates that the previous value symbol repeats 0 to any number of times; + indicates the previous value symbol repeats 1 to any number of times [A-z] denotes any uppercase letter*/ //2. Syntax/character description table//Value Expression /* . Match any character except a newline \w a character that matches a letter or number \w matches any character that is not a letter or number \s matches any of the whitespace characters (null, tab, line break) \s matches any character that is not a whitespace \d matches any number \ D matches any non-numeric character \b matches the end of a word or the beginning of a character \b matches any character that is not the end or beginning of a word [^x] matches any character that is not X. If [^[a-z]] matches any character of a non-lowercase letter ^ matches the end of the string at the beginning of the $ match string*/ //Modified Expression /** Match repeat any number of times + match repeat more than once? Match once or 0 times {n} matches repeat n times {n,} match repeats n times or n times more than {n,m} matches repeat at least n times Max M times*/[self.testbtn addtarget:self Action: @selector (Testbtnclick:) forcontrolevents:uicontroleventtouchupinside];} - (void) Testbtnclick: (uibutton*) btn{NSLog (@"Start Testing");//nsstring *regexp = @ "^\\d{5,8}"; //Match 5 to 8 digitsNSString *regexp =@"^\\d{1,}\\.\\d[1-9]";//match numbers less than or equal to 0.01Nspredicate *predicate = [Nspredicate predicatewithformat:@"Self MATCHES%@", REGEXP]; if([predicate evaluateWithObject:self.textTf.text]) {Self.label.text=@"Match Success"; }Else{Self.label.text=@"Match failed"; } }

Regular expressions for iOS (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.