IPhone development-obtain the content in a string using a regular expression

Source: Internet
Author: User

Origin:

To obtain the specified character in a string, use a regular expression and write the following code:


[Cpp] NSString * htmlStr = @ "oauth_token = token & oauth_token_secret = 3118a84ad942567990ba50f5649632fa & name = foolshit ";
NSString * regexString = @ "oauth_token = (\ w +) & oauth_token_secret = (\ w +) & name = (\ w + )";
NSString * matchedString1 = [htmlStr stringByMatching: regexString capture: 1L];
NSString * matchedString2 = [htmlStr stringByMatching: regexString capture: 2L];
NSString * matchedString3 = [htmlStr stringByMatching: regexString capture: 3L];
NSString * htmlStr = @ "oauth_token = token & oauth_token_secret = 3118a84ad942567990ba50f5649632fa & name = foolshit ";
NSString * regexString = @ "oauth_token = (\ w +) & oauth_token_secret = (\ w +) & name = (\ w + )";
NSString * matchedString1 = [htmlStr stringByMatching: regexString capture: 1L];
NSString * matchedString2 = [htmlStr stringByMatching: regexString capture: 2L];
NSString * matchedString3 = [htmlStr stringByMatching: regexString capture: 3L]; the result is as follows:

1a1de4ed4fca40599c5e5cfe0f4fba97

3118a84ad942567990ba50f5649632fa

Foolshit

Thoughts:

Although the task has been completed, this writing is obviously inefficient, because the regular expression must be enabled for each query. The improvements are as follows:

 


[Cpp] NSArray * matchArray = NULL;
MatchArray = [htmlStr componentsMatchedByRegex: regexString];
NSLog (@ "matchedString0 is % @", [matchArray objectAtIndex: 0]);
NSLog (@ "matchedString1 is % @", [matchArray objectAtIndex: 1]);
NSLog (@ "matchedString2 is % @", [matchArray objectAtIndex: 2]);
NSLog (@ "matchedString3 is % @", [matchArray objectAtIndex: 3]);
NSArray * matchArray = NULL;
MatchArray = [htmlStr componentsMatchedByRegex: regexString];
NSLog (@ "matchedString0 is % @", [matchArray objectAtIndex: 0]);
NSLog (@ "matchedString1 is % @", [matchArray objectAtIndex: 1]);
NSLog (@ "matchedString2 is % @", [matchArray objectAtIndex: 2]);
NSLog (@ "matchedString3 is % @", [matchArray objectAtIndex: 3]);

The result is as follows:

Oauth_token = 1a1de4ed4fca40599c5e5cfe0f4fba97 & oauth_token_secret = 3118a84ad942567990ba50f5649632fa & name = foolshit


1a1de4ed4fca40599c5e5cfe0f4fba97

3118a84ad942567990ba50f5649632fa

Foolshit

 


Note:

I want to get it in the form of $1, $2 getting success, but it is not successful. I don't know which expert can implement it.


 

From the column zcl316369

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.