iOS中正則表達式的使用

正則表達式在iOS開發中的應用

正則表達式在字符串查找,替換,檢測中的應用很是普遍,正則表達式是什麼,有怎樣的語法,個人另外一篇博客中有詳細的介紹:http://my.oschina.net/u/2340880/blog/403508。這裏只簡單說一下其概念 ,正則表達式是一種語法小巧簡單的語言,用來約束一些過濾字符串條的條件。不少開發工具都有支持正則表達式的內容,IOS也不例外,在IOS中NSRegularExpression類就是一個專門來處理正則表達式的類。正則表達式

1、初始化方法

初始化NSRegularExpression的方法有兩種,一個init方法和一個類方法。其做用基本是同樣的數組

+ (NSRegularExpression *)regularExpressionWithPattern:(NSString *)pattern options:(NSRegularExpressionOptions)options error:(NSError **)error;函數

- (instancetype)initWithPattern:(NSString *)pattern options:(NSRegularExpressionOptions)options error:(NSError **)error 工具

 

其中,pattern是正則表達式,options是參數。對於option參數,它是一個枚舉,表示正則模式的設置,以下:學習

typedef NS_OPTIONS(NSUInteger, NSRegularExpressionOptions) {
   NSRegularExpressionCaseInsensitive             = 1 << 0, //不區分字母大小寫的模式
   NSRegularExpressionAllowCommentsAndWhitespace  = 1 << 1, //忽略掉正則表達式中的空格和#號以後的字符
   NSRegularExpressionIgnoreMetacharacters        = 1 << 2, //將正則表達式總體做爲字符串處理
   NSRegularExpressionDotMatchesLineSeparators    = 1 << 3, //容許.匹配任何字符,包括換行符  
   NSRegularExpressionAnchorsMatchLines           = 1 << 4, //容許^和$符號匹配行的開頭和結尾
   NSRegularExpressionUseUnixLineSeparators       = 1 << 5, //設置\n爲惟一的行分隔符,不然全部的都有效。
   NSRegularExpressionUseUnicodeWordBoundaries    = 1 << 6 //使用Unicode TR#29標準做爲詞的邊界,不然全部傳統正則表達式的詞邊界都有效
};

注意:一、NSRegularExpressionCaseInsensitive模式下正則表達式 aBc 會匹配到abc.開發工具

            二、NSRegularExpressionIgnoreMetacharacters模式下正則表達式a b c 會匹配到abc,正則表達式ab#c會匹配到ab。ui

            三、NSRegularExpressionAllowCommentsAndWhitespace模式下正則表達式[a-z],會匹配到[a-z]。spa

2、獲取查詢結果

初始化完畢正則表達式的處理類後,咱們須要進行正則表達式的查詢,IOS官方提供了兩種模式:.net

一、帶block模式的方法:

- (void)enumerateMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range usingBlock:(void (^)(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop))block;code

使用舉例:

NSRegularExpression * regex = [[NSRegularExpression alloc]initWithPattern:@"[a-z]" options:NSRegularExpressionCaseInsensitive error:nil];
    [regex enumerateMatchesInString:@"124a" options:NSMatchingReportProgress range:NSMakeRange(0, 4) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        NSLog(@"%@",result);
    } ];

注意:一、這個函數的一個參數options是一個枚舉,設置回調的方式,以下:

typedef NS_OPTIONS(NSUInteger, NSMatchingOptions) {
   NSMatchingReportProgress         = 1 << 0, //找到最長的匹配字符串後調用block回調
   NSMatchingReportCompletion       = 1 << 1, //找到任何一個匹配串後都回調一次block
   NSMatchingAnchored               = 1 << 2, //從匹配範圍的開始出進行極限匹配
   NSMatchingWithTransparentBounds  = 1 << 3, //容許匹配的範圍超出設置的範圍
   NSMatchingWithoutAnchoringBounds = 1 << 4  //禁止^和$自動匹配行仍是和結束
};

            二、block回調中的flags枚舉對應以下:

typedef NS_OPTIONS(NSUInteger, NSMatchingFlags) {
   NSMatchingProgress               = 1 << 0, //匹配到最長串是被設置     
   NSMatchingCompleted              = 1 << 1, //所有分配完成後被設置    
   NSMatchingHitEnd                 = 1 << 2, //匹配到設置範圍的末尾時被設置   
   NSMatchingRequiredEnd            = 1 << 3, //當前匹配到的字符串在匹配範圍的末尾時被設置     
   NSMatchingInternalError          = 1 << 4  //因爲錯誤致使的匹配失敗時被設置   
};

            三、還有一點須要注意,就是那個bool值stop,咱們能夠在block塊中設置它爲YES,以後便會中止查找。

二、非block的方法

這個方法會返回一個結果數組,將全部匹配的結果返回

 

- (NSArray *)matchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;

 

這個方法會返回匹配到得字符串的個數

- (NSUInteger)numberOfMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;

 

這個方法會返回第一個查詢到得結果,這個NSTextCheckingResult對象中有一個range屬性,能夠獲得匹配到的字符串的範圍。

- (NSTextCheckingResult *)firstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;

 

這個方法直接返回匹配到得範圍,NSRange。

- (NSRange)rangeOfFirstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;

 

3、一個輔助方法

在NSRegularExpression類中還提供了一個輔助方法:

 

+ (NSString *)escapedPatternForString:(NSString *)string;

它能夠幫助咱們將正則表達式加上"\"進行保護,將元字符轉化成字面值。

 

到此,在IOS中正則表達式的基本用法就介紹完了,但願正則表達式的應用,能爲你的項目節省更多時間。

 

疏漏之處 歡迎指正

學習使用 歡迎轉載

 

專一技術,熱愛生活,交流技術,也作朋友。

——琿少 QQ羣:203317592

相關文章
相關標籤/搜索