1. What is a regular expression
Regular expressions, also known as normal notation, regular notation (English: Regular Expression, often abbreviated regex, regexp, or RE in code), are a concept in computer science.
Regular expressions use a single string to describe and match strings in series 1 that conform to certain syntactic rules.
In many text editors, regular expressions are used to retrieve and replace text that fits a pattern.
2. Character composition of regular expressions
Ordinary characters [a~z], special characters (called “metacharacters”)
3. Support
Almost all programming languages support regular expressions, such as OC, swift, java, c#, python, js, and so on
Regular expressions can be used for retrieval in many text editors, and Xcode also supports regular expressions!
Use 4.
matching
(pattern) matches pattern and gets the 1 match, which can be obtained from the resulting Matches set
A collection of
[xyz] character set (x|, |, y|, |, z) [a-z] character range a-z [a-zA-Z] character range a-z A-Z [^xyz] negative character set (any character except xyz) [^ a-z] negative character range [a-d][m-p] union (a to d or m to p)
Common metacharacter
Matches any character other than a newline
\w to match letters or Numbers or underline or Chinese characters [a-zA-Z_0-9] \s matches any blank character (space, TAB\t, carriage return \r \n) \d matching Numbers [0-9] ^a matches the a character at the beginning of the string a$matches the end of the a character string \bw matches the start or end of a word with w characters
Often use antisense characters
\W matches any character other than letter, number, underscore, Chinese character [^\w] \S matches any character that is not a blank [^\s] \D matches any non-numeric character [^0-9] \Ba matches a characters that are not at the beginning or end of a word [^a] matches any character other than a [^aeiou] matches any character other than the letters aeiou
Common qualifier
w*oo zero or more times w+oo repeat 1 or more times w & # 63; oo zero or one repeat w{n} w repeat n times w{n,} w repeat n or more w{n,m} w repeat n to m times
Greed and laziness
* & # 63; Repeat as many times as possible, but with as few repetitions as possible *+ repeat 1 or more times, but as little as possible The & # 63; The & # 63; Repeat 0 or 1 times, but as little as possible w} {1, 2 & # 63; Repeat 1 or 2 times, but as little as possible ww {1} & # 63; Repeat more than once, but as little as possible
Example 5.
// judge QQ Number (routine judgment)
fileprivate func checkIsQQNumber(str:String) ->Bool {
// 1. Judge whether 0 At the beginning
if str.hasPrefix("0"){
return false
}
// 2. Judge whether 5~15 position
if str.characters.count < 5 || str.characters.count > 15{
return false
}
// 3. Determine if it's all Numbers
for c in str.characters{
if c < "0" || c > "9"{
return false
}
}
return true
}
// Regex determines the phone number
fileprivate func checkPhoneNumber(str:String)->Bool {
let pattern = "1[3578]\\d{9}"
let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue:0))
let res = regex.matches(in: str, options: NSRegularExpression.MatchingOptions(rawValue:0), range: NSMakeRange(0, str.characters.count))
if res.count > 0 {
return true
}
return false
}
conclusion