The use of ios-regular expressions __ Regular expressions

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 '))
    if (flag)
        NSLog (@ "string is all numeric");
        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.

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

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

NSString * pattern = @ "[0-9][0-9]";
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 = @ "\\d\\d";
Of course I want to find a two-digit string, and we can do that.

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

NSString * pattern = @ "\\d{2}a{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-4}";
The following is a matching string containing the number of numbers 0 or one, the following matches the empty string is also included in the

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

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

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

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

NSString * pattern = @ "1*";
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 = @ "^\\.*\\d$";

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

NSString * str = @ "1121212312123";
NSString * pattern = @ "^[1-9]\\{5,11}$";
"+" 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?

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.