The practice of expression in IOS development (i)

Source: Internet
Author: User

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)

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.