Origin:
To get the character specified in the string, consider using a regular expression, and write the following code:
[CPP]View Plaincopy
- NSString *htmlstr = @"oauth_token=1a1de4ed4fca40599c5e5cfe0f4fba97&oauth_token_secret= 3118a84ad910967990ba50f5649632fa&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 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
- 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 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