iOS intercepts specific strings (regular matches)

Source: Internet
Author: User

Sometimes we have a need to intercept other strings from a string, depending on the situation, we analyze several methods ~ ~

I. strings in fixed-length strings that intercept fixed-position lengths

// 这是比较简单的一种情况:比如截取手机号的后4位 let phoneNum = "18515383061" var suffixNum:String? // 从倒数第四位开始截取,截取到最后 suffixNum = phoneNum.substringFromIndex(phoneNum.endIndex.advancedBy(-4)) // 从开头截取到第三位,获取手机号前3位 let prefixNum = phoneNum.substringToIndex(phoneNum.startIndex.advancedBy(3)) // 截取区间内字符串 suffixNum = phoneNum.substringWithRange(phoneNum.endIndex.advancedBy(-4)..<phoneNum.endIndex)

two. non-fixed-length strings, but with delimiters

 //例如获取日期中的年,月,日 // 分割符可以是任意的字符,一般为‘/‘,‘_‘,‘空格‘,或者是特殊的字符. let timeStr = "2013/10/26" let timeArr = timeStr.componentsSeparatedByString("/") print(timeArr)

three. non-fixed-length string, take a string under special rules

 //As shown below, we want to intercept the string inside the first bracket//assuming that the string is returned by the server, the length of the variable, the position of the brackets is also uncertain, successively through a simple interception is more difficult// This is the time to use the * * Regular expression * *, I believe you know, but how to use the regular expression in Swift to filter the value, let us analyze//rangofstring is used to collect the string in the text, but you can choose the mode. Here choose (. Regularexpressionsearch) that is the regular search//But OC and Swift have only this method of receiving, only search, no other, compared to other languages (python,php) Weak too much// Simple match in parentheses in the word is presumably everyone will write "\\[.*\\", but there is a problem is that the content is ' [thing] jflsdfs [do] ', which is obviously not what we want//this will receive a regular greedy mode, by default it matches as many strings as required , and we want him to meet the most delicate that, we need to add a number, that is, "\\[.*?\\", so the search is ' [thing] '//You find that this is not what we want, why bring ' [' and '] ', but no way, this is your search conditions AH// But nothing is too hard to pour, the regular is 0 wide assertion,< 0 width positive lookahead assertion (? =exp) > Assert itself where the occurrence of the position behind can match the expression exp,//< 0 width is recalling after the assertion (? <=exp); It asserts that the front of the position itself appears to match the expression exp, and finally our expression is "(? <=\\[)." =\\]) "Let string =" I want to does some [thing] jflsdfs [do] "if let result = String.rangeofstring (" (? <=\\[). *? =\\]) ", Options:. Regularexpressionsearch, Range:string.startIndex. <string.endindex, Locale:nil) {print (String.substringwithrange (Result)}}  


Ssbun
Links: Http://www.jianshu.com/p/d6fcc9bcd8de
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

iOS intercepts specific strings (regular matches)

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.