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