Abstract Requirements:
Now there's a string
{"state":200,"error":"登录成功!","json":{"auid":"402888f54c12441e014c1246bdf90003"}}
Now you need to extract the content string behind Auid, which 402888f54c12441e014c1246bdf90003 is
Analysis:
Before I learned the regular expressions, I used the following methods:
Go to the dictionary with Json and get to key.
For this, if you want to get auid corresponding to the content, I have two ways:
1. Do two times the JSON-to-dictionary.
2. One-time dictionary + one-time string interception
But both of these methods are cumbersome, so we think of the secret method of dealing with strings-regular expressions
Implementation steps:
1. Copy the original string to the regular tool (now many online regular test tools, I use REGEXRX)
2. Test Match
3.iOS Code Implementation
Note: Although the regular itself is generic, the processing of each language is different, like just the expression, in IOS, \d need to add the escape character \
The final code is implemented as follows:
//regex//1. Creating a regular Expression object and specifying a regular expressionNsregularexpression *regex = [Nsregularexpression regularexpressionwithpattern:@"[\\d\\w]{10,}"Options: 0ErrorNil];//2. Getting the range of a specific stringNstextcheckingresult *match = [Regex firstmatchinstring:stringOptions: 0 Range: Nsmakerange(0, [stringLength]);//3. Intercepting a specific stringif(match) {nsstring* result = [stringSubstringwithrange:match.Range];DD Logverbose (@"%@", result);//Store to Preferences[[Nsuserdefaults standarduserdefaults] Setobject:result forkey:@"Audi"]; }
Conclusion:
This time it's just a simple match, and I'm communicating with the server and I know what the data format must be. All work should be done from the demand. It is not necessary or possible to do everything.
 
The practice of expression in IOS development (i)