zoukankan      html  css  js  c++  java
  • 正则表达式

    在 4.0 之后,系统就有了它自己的类(NSRegularExpressionNSRegularExpression)来使用正则表达式,,之前都是要添加第三方类库 RegexKitLite 来使用

    这两个类的简单使用:

    [cpp] view plaincopy
     
    1. NSString *str = @"3sdfh*odsi";  
    2.       
    3.     //匹配第一个字符是数字  
    4.     NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:@"\b\d.*" options:0 error:nil];  
    5.     if (regex1 != nil) {  
    6.         NSTextCheckingResult *result1 = [regex1 firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];  
    7.         if (result1) {  
    8.             NSLog(@"第一个是数字");  
    9.         }else{  
    10.             NSLog(@"第一个不是数字");  
    11.         }  
    12.     }  
    13.     //匹配特殊字符  W     (W是大写)匹配任意不是字母,数字,下划线,汉字的字符  
    14.     NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@".*\W.*" options:0 error:nil];  
    15.     if (regex2) {  
    16.         NSTextCheckingResult *result2 = [regex2 firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];  
    17.         if (result2) {  
    18.             NSLog(@"有特殊字符");  
    19.         }else{  
    20.             NSLog(@"没有特殊字符");  
    21.         }  
    22.     }  
  • 相关阅读:
    常用的字符集编码
    live555—VS2010/VS2013 下live555编译、使用及测试(转载)
    win32下Socket编程(转载)
    do{...}while(0)的意义和用法(转载)
    C++ static与单例模式
    MFC多线程各种线程用法 .
    a^1+b problem
    重返现世——题解
    第K大C
    懒癌
  • 原文地址:https://www.cnblogs.com/leevaboo/p/3478123.html
Copyright © 2011-2022 走看看