Source: Internet
Author: User

In general, we use regular expressions in iOS. There are several steps 1, create a regular expression object to define rule 2, and use the regular Expression object to test the corresponding string.

We might have matched the string in the previous match, take out the string through a for loop, and then make a one by one match

NSString * str = @ "nsjkan1234980"; BOOL flag = YES; for (int i=0;i<str.length;i++) { Unichar c = [str characteratindex:i]; if (!) ( c>= ' 0 ' &&c<= ' 9 ')) { flag=no; break; } } if (flag) { NSLog (@ "string is all numeric"); } else { NSLog (@ "string contains non-numeric"); }

In Xcode we can use nsregularexpression * regex = [[Nsregularexpression alloc]initwithpattern:pattern options:0 Error:nil] ; One of the

The options represent Nsregularexpressionoptions, which is an enumeration that has the following values nsregularexpressioncaseinsensitive = 1 << 0 ,//Do not distinguish between uppercase and lowercase patterns

Nsregularexpressionallowcommentsandwhitespace = 1 << 1,//ignore the characters in the regular expression after the spaces and # numbers

Nsregularexpressionignoremetacharacters = 1 << 2,//The regular expression is treated as a string

nsregularexpressiondotmatcheslineseparators = 1 << 3,//Allow. Match any character, including line feed

Nsregularexpressionanchorsmatchlines = 1 << 4,//allow ^ and $ symbols to match the beginning and end of a line

nsregularexpressionuseunixlineseparators = 1 << 5,//set \ n is the only row delimiter, otherwise all are valid.

Nsregularexpressionuseunicodewordboundaries = 1 << 6//Use the Unicode tr#29 standard as the boundary of the word, otherwise all traditional regular expressions have valid word boundaries

For example, we can do this by matching the number of numbers in the STR string, where pattern is a rule, [0-9] is to look for numbers, and of course the preceding one is the same as [0123456789], because [] is to find the characters in this bracket.

NSString * str = @ "AAABBB"; NSString * pattern = @ "[0-9]"; nsregularexpression * regex = [[Nsregularexpression alloc]initwithpattern:pattern options:0 Error:nil]; Nsarray * results = [regex matchesinstring:str options:0 range:nsmakerange (0, Str.length)]; NSLog (@ "%d", results.count);

If it's an ordinary string, for example, the following AB is to match the string with no AB characters.

NSString * pattern = @ "AB";

Here's to find the number of all lowercase letters in the string.

If you want to find lowercase and uppercase letters can have, use the following.NSString * pattern = @ "[A-z]";

If you want to find two numbers that are connected together, you can use the following methodNSString * pattern = @ "[a-za-z]"; NSString * pattern = @ "[a-z| A-z] ";

Of course, we can write this, the number can be replaced by \d, of course, because of \ is the escape character, so we also need to add a \ To convert to the normal \NSString * pattern = @ "[0-9][0-9]";

Of course I want to find a two-digit string, and we can do that.NSString * pattern = @ "\\d\\d";

If this is the next one, find two digits followed by a substring of two A.NSString * pattern = @ "\\d{2}";

This is the number of numbers is 2-4, here should be noted that if our string is 7789, the match is 1 is not 2, because it can match how many matches how many.NSString * pattern = @ "\\d{2}a{2}";

The following is a matching string containing the number of numbers 0 or one, the following matches the empty string is also included in theNSString * pattern = @ "\\d{2-4}";

The following + number indicates a string that matches at least one number, there are two, one is 11212, and the other is 123.NSString * pattern = @ "\\d?";

The following is a string that starts with a numberNSString * str = @ "a11212abc123"; NSString * pattern = @ "\\d+";

The following is whether to start with two digitsNSString * pattern = @ "^\\d";

The following is a string of any 1, * representing anyNSString * pattern = @ "^\\d{2}";

The following decision begins with a number and ends with a number. Represents an arbitrary character newline except for what is the beginning of what, what is the end of the $NSString * pattern = @ "1*";

NSString * pattern = @ "^\\.*\\d$";

To determine if it is a pure number, and the beginning is not 0, the number is 6-11 bits

"+" can be represented by {1,}, "?" Can be expressed in {0,1}, in fact, that is to say that if the n+ is to contain at least one n character, if it is a * means that contains 0 or more characters N, if it is n?NSString * str = @ "1121212312123"; NSString * pattern = @ "^[1-9]\\{5,11}$";

Represents a string that matches 0 or 1 N. To express or mean. Regular expressions can be used to determine whether a string conforms to a particular rule

You can intercept the content of a particular rule in a string. The purpose of this is to add () the meaning of the grouping, where the content represents a string, () itself does not match anything, and does not restrict the matching of anything, but the contents of parentheses are treated as one expression, for example (AB) {1,3 It means that AB appears at least 1 times in a row, up to 3 times. Without parentheses, ab{1,3}, which means a, followed by a minimum of 1 times, up to 3 times.