Import UIKit/*Exercise 1: Match ABC Exercise 2: Include a a~z, followed by 0~9-->[a-z][0-9] or [a-z]\d * [A-Z]: A~z * [0-9]/\d:0~9:[0-9]/\d: means [0-9] or \d,\d represents The meaning of the numbers Exercise 3: Must first be the letter, the second is the number-->^[a-z][0-9]$ * ^[a-z]: Indicates that the initial letter must be A~z * \d{2,10}: The number is 2 to ten * [a-z]$: Indicates that a A-Z letter must be terminated. Xi 4: Must be the first letter, followed by the letter 4~9 number Exercise 5: Cannot be the number 0-9 * [^0-9]: Can not be 0~9 exercise 6:QQ match: ^[1-9]\d{4,11}$ is a digital 5~12 bit and the first bit can not be 0 Exercise 7: Phone number matching ^1[3578] \d{9}$1. To 13/15/17/182. Length is one*/classViewcontroller:uiviewcontroller {Overridefunc viewdidload () {super.viewdidload () let str="13324132423" //1. Create Regular expression rulesLet pattern ="^1[3578]\\d{9}$"//\\d{9}, where \ \ means escape, \\d{9}, followed by 9 digits//2. Create a regular Expression object (throw exception throw: Try try? try!)Guard Let regex =Try? Nsregularexpression (Pattern:pattern, Options: [])Else { return } //3. Match the contents of the string: the length of the string in Swift: Str.characters.countLet results = regex.matches (inch: STR, options: [], Range:nsrange (location:0, Length:str.characters.count)) //4. Iterate through the array to get the result: the interception of a string is generally handled by converting Swift's string into OC's string forResultinchResults {print (str asnsstring). substring (with:result.range)) print (Result.range) }}}
Import UIKitclassViewcontroller:uiviewcontroller {Overridefunc viewdidload () {super.viewdidload () let StatusText="@coderwhy: "Animal Scream Collection" #肥猪流 # The owl is screaming [laughing], @m A J: the mouse screams, the rabbit screams [surprised], @ Flower full floor: Inexplicable # Small cage bag # wonderful smile to the last [Good love OH]! ~ http://t.cn/zYBuKZ8/" /*1: Match out @coderwhy:let pattern = "@.*?:", where. * used to represent any character, plus? To indicate that as soon as it encounters: Stop the match immediately, no? , it will always match to the last one: 2: Match expression: let pattern = "\\[.*?\\]", because [], there is a special meaning, so to escape, escaped with \ escaped, \ In the SIWFT string also has a special meaning, so then use \ to Indicates that the escape is used \, if escaped, there is a special meaning, then use \ to represent **/ //1. Create a matching rule//Let pattern = "@.*?:"//match out @coderwhy://Let pattern = "#.*?#"//Matching Topics//Let pattern = "\\[.*?\\]"//Match ExpressionLet pattern ="http (s)?:/ /([\\w-]+\\.) +[\\w-]+ (/[\\w-./?%&=]*)?" //URL URLs//2. Creating a regular Expression objectGuard Let regex =Try? Nsregularexpression (Pattern:pattern, Options: [])Else { return } //3. Start MatchingLet results = regex.matchesinstring (statustext, Options: [], Range:nsrange (location:0, Length:statusText.characters.count)) //4. Get Results forResultinchResults {print (StatusText asnsstring). Substringwithrange (Result.range)} }}
Swift Project Nineth Day: Regular expression Learning