iphone development--Regular expressions get the contents of a string

Source: Internet
Author: User

Origin:

To get the character specified in the string, consider using a regular expression, and write the following code:

[CPP]View Plaincopy
    1. NSString *htmlstr = @"oauth_token=1a1de4ed4fca40599c5e5cfe0f4fba97&oauth_token_secret=  3118a84ad910967990ba50f5649632fa&name=foolshit ";
    2. NSString *regexstring = @"oauth_token= (\\w+) &oauth_token_secret= (\\w+) &name= (\\w+)";
    3. NSString *matchedstring1 = [Htmlstr stringbymatching:regexstring capture:1l];
    4. NSString *matchedstring2 = [Htmlstr stringbymatching:regexstring capture:2l];
    5. NSString *matchedstring3 = [Htmlstr stringbymatching:regexstring capture:3l];

The results obtained are as follows:

1a1de4ed4fca40599c5e5cfe0f4fba97

3118a84ad910967990ba50f5649632fa

Foolshit

Thinking:

Although the task is completed, it is obviously inefficient to write because regular expressions are required for each fetch. So the improvements are as follows:

[CPP]View Plaincopy
    1. Nsarray *matcharray = NULL;
    2. Matcharray = [Htmlstr componentsmatchedbyregex:regexstring];
    3. NSLog (@"MATCHEDSTRING0 is%@", [Matcharray objectatindex:0]);
    4. NSLog (@"matchedString1 is%@", [Matcharray objectatindex:1]);
    5. NSLog (@"MatchedString2 is%@", [Matcharray objectatindex:2]);
    6. NSLog (@"MatchedString3 is%@", [Matcharray Objectatindex:3]);

The results obtained are as follows:

Oauth_token=1a1de4ed4fca40599c5e5cfe0f4fba97&oauth_token_secret=3118a84ad910967990ba50f5649632fa&name= Foolshit

1a1de4ed4fca40599c5e5cfe0f4fba97

3118a84ad910967990ba50f5649632fa

Foolshit

Note:

I want to get through this form of $1,$2, but it's not successful. Do not know which master can achieve.

Appendix: Forgot to write need to join the third party class library, ⊙﹏⊙ b Khan, thanks to a message reminder.

1. Download the Regexkitlite class library, extract it will have an example package and 2 files (Regexkitlite folder two files), in fact, the use of the 2 files, added to the project.

2. Add Libicucore.dylib frameworks to the project.

Ext.: http://blog.csdn.net/zcl369369/article/details/7181807

iphone development--Regular expressions get the contents of a string

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.