正則表達式(regular expression)描述了一種字符串匹配的模式(pattern),可以用來檢查一個串是否含有某種子串、將匹配的子串替換或者從某個串中取出符合某個條件的子串等。
^ 指出一個字符串的開始
$指出一個字符串的結(jié)束
"^iOS" 以iOS開頭
"iOS$" 以iOS結(jié)尾
"^apple$" 開始和結(jié)尾都是apple的字符串,這個是唯一的,實際上就是apple
"apple" 包含apple
* , +,? 重復(fù)出現(xiàn)的次數(shù)。 ? 0~1; + 1~n; * 0~n
"ab*"一個a后面跟著0~n個b
"ab+":一個a后面跟著至少一個b
"ab?":一個a后面跟著0~1個b
"a?b+$":末尾有01個a跟著1n個b
{} 表示重復(fù)的具體范圍。
"ab{4}" 一個a跟著4b
"ab{1,}" 一個a跟著至少1個b
"ab{3,5}" 一個a跟著3~5個b
*可以用{0,}表示,+可以用{1,}表示,?可以用{0,1}表示
| “或”操作:
"a|b" 一個字符串里有a或b
"(a|bcd)ef" aef或bcdef
"(a|b)+c" 一串a(chǎn)和b混合的字符串后面跟一個c;
[ ]表示在括號內(nèi)的眾多字符中,選擇1-n個括號內(nèi)的符合語法的字符作為結(jié)果
"[ab]" 一個a或b(相當于"a|b");
"[a-d]"a到d中的一個(相當于"a|b|c|d"或者"[abcd]")
"^[a-zA-Z]" 以字母開頭
"[0-9]a" a前有一位的數(shù)字
"[a-zA-Z0-9]$" 以一個字母或數(shù)字結(jié)束
. 任意字符
"a.[a-z]" a后面跟一個任意字符和一個小寫字母
"^.{5}$" 長度為5的字符串
"(.)\1" 兩個連續(xù)任意字符
"(.)\1{2}" 三個個連續(xù)任意字符
在方括號里用^表示不希望出現(xiàn)的字符,^應(yīng)在方括號里的第一位。
"@[^a-zA-Z]@"表示兩個@中不應(yīng)該出現(xiàn)字母
"\d" 一個數(shù)字字符
"\D" 一個非數(shù)字字符
"\w " 包括下劃線的任何單詞字符
"\W" 匹配任何非單詞字符
iOS中書寫正則表達式,碰到轉(zhuǎn)義字符,多加一個\
正則表達式在IOS開發(fā)中的應(yīng)用
一、根據(jù)正則表達式創(chuàng)建NSRegularExpression對象
初始化方法
<pre>
public init(pattern: String, options: NSRegularExpression.Options = []) throws
</pre>
其中,pattern是正則表達式,對于option參數(shù),在oc中它是一個枚舉,swift中它是結(jié)構(gòu)體options中的屬性
<pre>
public struct Options : OptionSet {
public init(rawValue: UInt)
public static var caseInsensitive: NSRegularExpression.Options { get }
public static var allowCommentsAndWhitespace: NSRegularExpression.Options { get }
public static var ignoreMetacharacters: NSRegularExpression.Options { get }
public static var dotMatchesLineSeparators: NSRegularExpression.Options { get }
public static var anchorsMatchLines: NSRegularExpression.Options { get }
public static var useUnixLineSeparators: NSRegularExpression.Options { get }
public static var useUnicodeWordBoundaries: NSRegularExpression.Options { get }
}
</pre>
<pre>
caseInsensitive //不區(qū)分字母大小寫
allowCommentsAndWhitespace //忽略掉正則表達式中的空格和#號之后的字符
ignoreMetacharacters //將正則表達式整體作為字符串處理
dotMatchesLineSeparators //允許.匹配任何字符,包括換行符
anchorsMatchLines //允許^和$符號匹配行的開頭和結(jié)尾
useUnixLineSeparators //設(shè)置\n為唯一的行分隔符,否則所有的都有效。
useUnicodeWordBoundaries //使用Unicode TR#29標準作為詞的邊界,否則所有傳統(tǒng)正則表達式的詞邊界都有效
</pre>
二、執(zhí)行查詢操作
1、以block的形式返回查詢結(jié)果
<pre>
open func enumerateMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, using block: (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void)
</pre>
參數(shù)string是待查詢的字符串,options的參數(shù)如下:
<pre>
public struct MatchingOptions : OptionSet {
public init(rawValue: UInt)
public static var reportProgress: NSRegularExpression.MatchingOptions { get }
public static var reportCompletion: NSRegularExpression.MatchingOptions { get }
public static var anchored: NSRegularExpression.MatchingOptions { get }
public static var withTransparentBounds: NSRegularExpression.MatchingOptions { get }
public static var withoutAnchoringBounds: NSRegularExpression.MatchingOptions { get }
}
</pre>
<pre>
reportProgress //找到最長的匹配字符串后調(diào)用block回調(diào)
reportCompletion //找到任何一個匹配串后都回調(diào)一次block
anchored //從匹配范圍的開始出進行極限匹配
withTransparentBounds //允許匹配的范圍超出設(shè)置的范圍
withoutAnchoringBounds //禁止^和$自動匹配行還是和結(jié)束
</pre>
參數(shù)block回調(diào)中,NSTextCheckingResult類型的參數(shù)即是查詢結(jié)果;MatchingFlags類型的參數(shù)flags如下:
<pre>
public struct MatchingFlags : OptionSet {
public init(rawValue: UInt)
public static var progress: NSRegularExpression.MatchingFlags { get }
public static var completed: NSRegularExpression.MatchingFlags { get }
public static var hitEnd: NSRegularExpression.MatchingFlags { get }
public static var requiredEnd: NSRegularExpression.MatchingFlags { get }
public static var internalError: NSRegularExpression.MatchingFlags { get }
}
</pre>
<pre>
progress //匹配到最長串時被設(shè)置
completed //全部分配完成后被設(shè)置
hitEnd //匹配到設(shè)置范圍的末尾時被設(shè)置
requiredEnd //當前匹配到的字符串在匹配范圍的末尾時被設(shè)置
internalError //由于錯誤導(dǎo)致的匹配失敗時被設(shè)置
</pre>
block中還有個類型為UnsafeMutablePointer<ObjCBool>的參數(shù),給它的pointee屬性賦值為true,之后便會停止查找
2、查找結(jié)果以數(shù)組的形式返回
<pre>
open func matches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> [NSTextCheckingResult]
</pre>
3、返回匹配到的個數(shù)
<pre>
open func numberOfMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> Int
</pre>
4、返回匹配到的第一個結(jié)果
<pre>
open func firstMatch(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> NSTextCheckingResult?
</pre>
5、返回匹配到的第一個結(jié)果的range
<pre>
open func rangeOfFirstMatch(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> NSRange
</pre>
6、將匹配到的結(jié)果替換為新的字符串,并將替換后生成的新的字符串返回
<pre>
open func stringByReplacingMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, withTemplate templ: String) -> String
</pre>
7、將原字符串中匹配到的結(jié)果用指定的字符串替換,返回值為Int類型,是匹配并替 換的個數(shù)
<pre>
open func replaceMatches(in string: NSMutableString, options: NSRegularExpression.MatchingOptions = [], range: NSRange, withTemplate templ: String) -> Int
</pre>