Home>

A regular expression (regexp) is a text character consisting of metacharacters and alphanumerics,Or a "text" (abc, 123, and others) text mode. This type is used to match text characters-with matching results,Success or failure. regexps is mainly used for regular text matching and search and replace.

What is a regular expression

Regular expression, in computer science,A single string that describes or matches a series of strings that conform to a certain syntax rule.In many text editors or other tools,Regular expressions are often used to retrieve and/or replace text content that matches a pattern.The concept of regular expressions was originally popularized by tools such as sed and grep in Unix.Regular expressions are usually abbreviated as "regex", singular has regexp, regex, plural has regexps, regexes, regexen.

Regular expression composition

Regular expressions consist of two types of characters

The first:the characters used to match,Or regular characters

Second:control characters or metacharacters with special meaning

The use of regular expressions has been supported since iPhone 4.0.The use of regular expressions in iOS 4.0 is called using the nsregularexpression class.

1. An example of a simple use of regular expressions:nsregularexpression class

-(void) parsestring {
//Assemble a string,Need to parse out the URL inside
nsstring * urlstring [email protected]"sfdsfhttp://www.google.com";
//nsregularexpression class calls the expression method needs to pass an nserror parameter. Define one below
 nserror * error;
//http +:[^ \\ s] * This expression detects 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];//equivalent to firstmatch.range --- matching range
   //Intercept data from urlstring
  nsstring * result=[urlstring substringwithrange:resultrange];
  //output result
  nslog (@ "%@", result);
  }
  }
}

2. Use regular expressions to determine

//Initialize an nsregularexpression object and set the detection object range to:0-9
nsregularexpression * regex2=[nsregularexpression regularexpressionwithpattern:@"^ [0-9] * $" options:0 error:nil];
    if (regex2)
    {//objects to match
       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

//Use regular expression validation

nspredicatel class:mainly used to specify filter conditions,This object can accurately describe the required conditions,Filter each object by a predicate,Determine if it matches the condition.A predicate is a function that represents the calculation of true and false values ​​in a computer.The principle and usage are similar to where in a sql query, and its role is equivalent to database filtering.It is mainly used to sort out eligible objects from the collection.Can also be used for regular matching of strings

-(bool) isvalidateemail:(nsstring *) email
{
  nsstring * emailregex [email protected]"[a-z0-9a-z ._%+-] [email protected][a-za-z0-9 .-] + \\. [a-za-z] {2,4}" ;
  nspredicate * emailtest=[nspredicate predicatewithformat:@"self matches%@", emailregex];
  return [emailtest evaluatewithobject:email];
}

2. Regular expression that matches 9-15 letters/digits:

 nsstring * regex [email protected]"^ [a-za-z0-9] {9,15} $";
  nspredicate * pred=[nspredicate predicatewithformat:@"self matches%@", regex];
  bool ismatch=[pred evaluatewithobject:txtfldphonenumber.text];

cocoa uses nspredicate to describe the query,The principle is similar to querying in a database

Use predicates such as between, in, beginwith, endwith, contains, like to construct nspredicate, and use self to directly match itself when necessary.

//Basic query
nspredicate * predicate;
predicate=[nspredicate predicatewithformat:@"name ==" herbie ""];
  bool match=[predicate evaluatewithobject:car];
  nslog (@ "%s", (match)?"yes":"no");
//Cycle comparison in the entire cars
  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);
//predicates containing variables
  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 that you cannot use $variable as the path name,Because its value represents the value
//Predicate character channeling also supports some common operators in C language
  predicate=[nspredicate predicatewithformat:
         @ "(engine.horsepower>50) 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];//This limits the scope of self
  nslog (@ "%@", results);
//beginswith, endswith, contains
//additional symbols,[c], [d], [cd], c means case-insensitive,d means indistinguishable pronunciation characters,cd means nothing
  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);
//like operator (wildcard)
  predicate=[nspredicate predicatewithformat:@"name like [cd]" * er * ""];
  results=[cars filteredarrayusingpredicate:predicate];
  nslog (@ "%@", results);
  predicate=[nspredicate predicatewithformat:@"name like [cd]"
  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");
//Cycle comparison in the entire cars
  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);
//predicates containing variables
  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 that you cannot use $variable as the path name,Because its value represents the value
//Predicate character channeling also supports some common operators in C language
  predicate=[nspredicate predicatewithformat:
         @ "(engine.horsepower>50) 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];//This limits the scope of self
  nslog (@ "%@", results);
//beginswith, endswith, contains
//additional symbols,[c], [d], [cd], c means case-insensitive,d means indistinguishable pronunciation characters,cd means nothing
  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);
//like operator (wildcard)
  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);
  • Previous C # P2P voice chat tool based on UDP
  • Next Analysis of methods of the TelephonyManager class in Android