NSRegularExpression

作者: Laughingg | 来源:发表于2016-09-29 15:00 被阅读311次
    extension NSRegularExpression {
        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 }
        }
    
    
        public struct MatchingOptions : OptionSet {
    
            public init(rawValue: UInt)
    
            
            public static var reportProgress: NSRegularExpression.MatchingOptions { get } /* Call the block periodically during long-running match operations. */
    
            public static var reportCompletion: NSRegularExpression.MatchingOptions { get } /* Call the block once after the completion of any matching. */
    
            public static var anchored: NSRegularExpression.MatchingOptions { get } /* Limit matches to those at the start of the search range. */
    
            public static var withTransparentBounds: NSRegularExpression.MatchingOptions { get } /* Allow matching to look beyond the bounds of the search range. */
    
            public static var withoutAnchoringBounds: NSRegularExpression.MatchingOptions { get } /* Prevent ^ and $ from automatically matching the beginning and end of the search range. */
        }
    
        
        public struct MatchingFlags : OptionSet {
    
            public init(rawValue: UInt)
    
            
            public static var progress: NSRegularExpression.MatchingFlags { get } /* Set when the block is called to report progress during a long-running match operation. */
    
            public static var completed: NSRegularExpression.MatchingFlags { get } /* Set when the block is called after completion of any matching. */
    
            public static var hitEnd: NSRegularExpression.MatchingFlags { get } /* Set when the current match operation reached the end of the search range. */
    
            public static var requiredEnd: NSRegularExpression.MatchingFlags { get } /* Set when the current match depended on the location of the end of the search range. */
    
            public static var internalError: NSRegularExpression.MatchingFlags { get } /* Set when matching failed due to an internal error. */
        }
    }
    @available(OSX 10.7, *)
    open class NSRegularExpression : NSObject, NSCopying, NSSecureCoding {
        // 用来创建正则表达式对象
        public init(pattern: String, options: NSRegularExpression.Options = []) throws
    
        // 匹配的模式字符串 
        open var pattern: String { get }
    
        open var options: NSRegularExpression.Options { get }
    
        open var numberOfCaptureGroups: Int { get }
    
        open class func escapedPattern(for string: String) -> String
    }
    
    

    匹配的方法

    extension NSRegularExpression {
        
        open func enumerateMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, using block: (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void)
    
        // 返回所有匹配结果的集合(适合,从一段字符串中提取我们想要匹配的所有数据)
        open func matches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> [NSTextCheckingResult]
    
        // 返回正确匹配的个数(通过等于0,来验证邮箱,电话什么的,代替NSPredicate)
        open func numberOfMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> Int
    
        //  返回第一个匹配的结果。注意,匹配的结果保存在  NSTextCheckingResult 类型中
        open func firstMatch(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> NSTextCheckingResult?
       // 返回第一个正确匹配结果字符串的NSRange
        open func rangeOfFirstMatch(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> NSRange
    }
    
    

    替换处理

    extension NSRegularExpression {
    
        open func stringByReplacingMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, withTemplate templ: String) -> String
    
        open func replaceMatches(in string: NSMutableString, options: NSRegularExpression.MatchingOptions = [], range: NSRange, withTemplate templ: String) -> Int
    
        
       
        open func replacementString(for result: NSTextCheckingResult, in string: String, offset: Int, template templ: String) -> String
    
        
        
        open class func escapedTemplate(for string: String) -> String
    }
    
    
    
    

    数据探测

    @available(OSX 10.7, *)
    open class NSDataDetector : NSRegularExpression {
    
        
        public init(types checkingTypes: NSTextCheckingTypes) throws
    
        
        open var checkingTypes: NSTextCheckingTypes { get }
    }
    
    

    相关文章

      网友评论

        本文标题:NSRegularExpression

        本文链接:https://www.haomeiwen.com/subject/uhmkyttx.html