Use regular expression nsregularexpression in iOS to validate textfiled input _ Regular expressions

Source: Internet
Author: User
Tags control characters regular expression

What is a regular expression

A regular expression (regular expression), in computer science, refers to a single string that describes or matches a series of strings that conform to a certain syntactic rule. In many text editors or other tools, regular expressions are often used to retrieve and/or replace text content that conforms to a pattern. The concept of regular expressions was initially popularized by tool software (such as SED and grep) in Unix. Regular expressions are usually abbreviated as "regex", singular with regexp, regex, plural with regexps, regexes, Regexen.

The regular expression is composed of

Regular expressions are made up of two types of characters

First: Used to match the characters, or the regular characters

Second: control characters or meta characters with special meanings

The iphone 4.0 has since been used to support regular expressions, and the use of ios4.0 expressions is invoked using the Nsregularexpression class.

1. One example of a simple use of regular expressions is as follows: Nsregularexpression class

-(void) parsestring{
//Assemble a string, need to parse out the inside URL
nsstring *urlstring=@ "sfdsfhttp://www.baidu.com";
The Nsregularexpression class calls the expression method to pass a nserror parameter. The following defines a
 nserror *error;
http+:[^\\s]* This expression is to detect a URL.
  nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:@ "http+:[^\\s]*" options:0 error :&error];
  if (regex!= nil) {
  nstextcheckingresult *firstmatch=[regex firstmatchinstring:urlstring options:0range: Nsmakerange (0, [urlstring length])];
  if (firstmatch) {
   Nsrange resultrange = [Firstmatch rangeatindex:0];//equal to Firstmatch.range---match range
   // Interception of data from URLString
  nsstring *result=[urlstring Substringwithrange:resultrange];
  Output
  NSLog (@ "%@", result);}
  }

2. Use regular expressions to determine

Initializes a Nsregularexpression object and sets the scope of the detection object to: 0-9 
nsregularexpression *regex2 = [nsregularexpression regularexpressionwithpattern:@ "^[0-9]*$" options:0 Error:nil];
    if (REGEX2)
    {//object is matched
       nstextcheckingresult *result2 = [Regex2 firstMatchInString:textField.text options:0 Range:nsmakerange (0, [textfield.text length])];
      if (RESULT2) {
      }
}}

1. Code to determine whether the mailbox format is correct: Nspredicatel class

Validating with regular expressions

Nspredicatel class: Mainly used to specify the conditions of the filter, the object can accurately describe the required conditions, each object through the predicate filter, to determine whether the conditions match. A predicate is a function that represents a calculated true or False value in a computer. The principle and usage are similar to the Where in the SQL query, which acts as a filter for the database. Used primarily to sort out objects that meet the criteria from the collection, or to match regular matches for strings

-(BOOL) Isvalidateemail: (NSString *) email
{
  NSString *emailregex = @ "[a-z0-9a-z._%+-]+@[a-za-z0-9.-]+\\.[ a-za-z]{2,4} ";
  Nspredicate *emailtest = [nspredicate predicatewithformat:@ "SELF matches%@", Emailregex];
  return [Emailtest Evaluatewithobject:email];
}

2. Match 9-15 regular expressions consisting of a letter/number string:

  NSString * regex = @ "^[a-za-z0-9]{9,15}$";
  Nspredicate *pred = [nspredicate predicatewithformat:@ "SELF matches%@", regex);
  BOOL IsMatch = [pred evaluateWithObject:txtfldPhoneNumber.text];

Cocoa uses nspredicate to describe a query in a way that is similar to querying in a database

Construct nspredicate with Between,in,beginwith,endwith,contains,like these predicates, and use self directly to match yourself when necessary

Basic Query Nspredicate *predicate; 
  predicate = [Nspredicate predicatewithformat: @ "name = = ' Herbie '"]; 
  BOOL match = [predicate evaluatewithobject:car]; NSLog (@ "%s", (match)? 
"YES": "NO"); 
  Inside the entire cars cycle compare predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower > 150"]; 
  Nsarray *cars = [garage cars]; 
    For (car *car in [garage cars]) {if ([predicate Evaluatewithobject:car]) {NSLog (@ "%@", car.name); 
  ///output complete information predicate = [nspredicate Predicatewithformat: @ "Engine.horsepower > 150"]; 
  Nsarray *results; 
  results = [cars filteredarrayusingpredicate:predicate]; 
NSLog (@ "%@", results); 
  predicate containing variable nspredicate *predicatetemplate = [nspredicate predicatewithformat:@ "name = = $NAME"]; 
  Nsdictionary *vardict; 
  Vardict = [Nsdictionary Dictionarywithobjectsandkeys: @ "Herbie", @ "NAME", nil]; 
  predicate = [Predicatetemplate predicatewithsubstitutionvariables:vardict]; 
  NSLog (@ "Snorgle:%@", predicate); Match = [predicate evaluatewithobject:car]; NSLog (@ "%s", (match)? 
"YES": "NO"); Note You cannot use $variable as the pathname because its value represents the value//predicate character channeling also supports some commonly used operators in c language predicate = [nspredicate Predicatewithformat: @ "(Engin 
  E.horsepower >) and (Engine.horsepower < 200) "]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
  NSLog (@ "OOP%@", results); 
  predicate = [Nspredicate predicatewithformat: @ ' name < ' Newton ']; 
  results = [cars filteredarrayusingpredicate:predicate]; 
NSLog (@ "%@", [Results Valueforkey: @ "name"]); 
  Powerful array operator predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower BETWEEN {50, 200}"]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
  NSLog (@ "%@", results); Nsarray *betweens = [Nsarray arraywithobjects: [NSNumber numberwithint:50], [NSNumber numberwithint:200], 
  NIL]; 
  predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower BETWEEN%@", Betweens]; results = [Cars FILTEREDARRAYUSINGPRedicate:predicate]; 
  NSLog (@ "%@", results); 
  Predicatetemplate = [Nspredicate predicatewithformat: @ "Engine.horsepower BETWEEN $POWERS"]; 
  Vardict = [Nsdictionary dictionarywithobjectsandkeys:betweens, @ "Powers", nil]; 
  predicate = [Predicatetemplate predicatewithsubstitutionvariables:vardict]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
NSLog (@ "%@", results); 
  In operator predicate = [Nspredicate predicatewithformat: @ "name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
  NSLog (@ "%@", [Results Valueforkey: @ "name"]); 
  predicate = [Nspredicate Predicatewithformat: @ "self.name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
  NSLog (@ "%@", [Results Valueforkey: @ "name"]); 
  names = [Cars Valueforkey: @ ' name ']; 
  predicate = [Nspredicate Predicatewithformat: @ "SELF in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"]; results = [names filteredarrayusingpredicate:predicate];//here limits the scope of self NSLog (@ "%@", results); Beginswith,endswith,contains//additional symbol, [c],[d],[cd],c indicates case-insensitive, d means no accent character, CD represents nothing distinguishable predicate = [nspredicate 
  Predicatewithformat: @ "name Beginswith ' bad '"]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
  NSLog (@ "%@", results); 
  predicate = [Nspredicate predicatewithformat: @ "name Beginswith ' HERB '"]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
  NSLog (@ "%@", results); 
  predicate = [Nspredicate predicatewithformat: @ "name BEGINSWITH[CD] ' HERB '"]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
NSLog (@ "%@", results); 
  The LIKE operator (wildcard character) predicate = [Nspredicate predicatewithformat: @ "name LIKE[CD] ' *er* '"]; 
  results = [cars filteredarrayusingpredicate:predicate]; 
  NSLog (@ "%@", results); predicate = [Nspredicate predicatewithformat: @ ' name LIKE[CD] '??? 
  Er* ' "]; 
  results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results);
Basic Query Nspredicate *predicate;
  predicate = [Nspredicate predicatewithformat: @ "name = = ' Herbie '"];
  BOOL match = [predicate evaluatewithobject:car]; NSLog (@ "%s", (match)?
"YES": "NO");
  Inside the entire cars cycle compare predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower > 150"];
  Nsarray *cars = [garage cars];
    For (car *car in [garage cars]) {if ([predicate Evaluatewithobject:car]) {NSLog (@ "%@", car.name);
  ///output complete information predicate = [nspredicate Predicatewithformat: @ "Engine.horsepower > 150"];
  Nsarray *results;
  results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
  predicate containing variable nspredicate *predicatetemplate = [nspredicate predicatewithformat:@ "name = = $NAME"];
  Nsdictionary *vardict;
  Vardict = [Nsdictionary Dictionarywithobjectsandkeys: @ "Herbie", @ "NAME", nil];
  predicate = [Predicatetemplate predicatewithsubstitutionvariables:vardict];
  NSLog (@ "Snorgle:%@", predicate); Match = [predicate EvaluatewiThobject:car]; NSLog (@ "%s", (match)?
"YES": "NO"); Note You cannot use $variable as the pathname because its value represents the value//predicate character channeling also supports some commonly used operators in c language predicate = [nspredicate Predicatewithformat: @ "(engine.h
  Orsepower >) and (Engine.horsepower < 200) "];
  results = [cars filteredarrayusingpredicate:predicate];
  NSLog (@ "OOP%@", results);
  predicate = [Nspredicate predicatewithformat: @ ' name < ' Newton '];
  results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", [Results Valueforkey: @ "name"]);
  Powerful array operator predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower BETWEEN {50, 200}"];
  results = [cars filteredarrayusingpredicate:predicate];
  NSLog (@ "%@", results); Nsarray *betweens = [Nsarray arraywithobjects: [NSNumber numberwithint:50], [NSNumber numberwithint:200], n
  IL];
  predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower BETWEEN%@", Betweens];
  results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results);
  Predicatetemplate = [Nspredicate predicatewithformat: @ "Engine.horsepower BETWEEN $POWERS"];
  Vardict = [Nsdictionary dictionarywithobjectsandkeys:betweens, @ "Powers", nil];
  predicate = [Predicatetemplate predicatewithsubstitutionvariables:vardict];
  results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
  In operator predicate = [Nspredicate predicatewithformat: @ "name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"];
  results = [cars filteredarrayusingpredicate:predicate];
  NSLog (@ "%@", [Results Valueforkey: @ "name"]);
  predicate = [Nspredicate Predicatewithformat: @ "self.name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"];
  results = [cars filteredarrayusingpredicate:predicate];
  NSLog (@ "%@", [Results Valueforkey: @ "name"]);
  names = [Cars Valueforkey: @ ' name '];
  predicate = [Nspredicate Predicatewithformat: @ "SELF in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"]; results = [names filteredarrayusingpredicate:predicate];//here limits the scope of self NSLog (@ "%@", results); Beginswith,endswith,contains//additional symbol, [c],[d],[cd],c indicates case-insensitive, d means no accent character, CD represents nothing distinguishable predicate = [nspredicate
  Predicatewithformat: @ "name Beginswith ' bad '"];
  results = [cars filteredarrayusingpredicate:predicate];
  NSLog (@ "%@", results);
  predicate = [Nspredicate predicatewithformat: @ "name Beginswith ' HERB '"];
  results = [cars filteredarrayusingpredicate:predicate];
  NSLog (@ "%@", results);
  predicate = [Nspredicate predicatewithformat: @ "name BEGINSWITH[CD] ' HERB '"];
  results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
  The LIKE operator (wildcard character) predicate = [Nspredicate predicatewithformat: @ "name LIKE[CD] ' *er* '"];
  results = [cars filteredarrayusingpredicate:predicate];
  NSLog (@ "%@", results); predicate = [Nspredicate predicatewithformat: @ ' name LIKE[CD] '???
  Er* ' "];
  results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results);

The above is a small series for everyone to share the iOS use regular expression nsregularexpression to verify the content of textfiled input, I hope you like.

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: info-contact@alibabacloud.com 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.