0001 public struct Regex: CustomStringConvertible, CustomDebugStringConvertible { 0002 0003 // MARK: Initialisation 0004 0005 internal let regularExpression
Regex.swift:74 Regex._lastMatch = matchRegex.swift:91 if let firstMatch = matches.first { Regex._lastMatch = firstMatch }Regex.swift:148 public func ~= (regex: Regex, string: String) -> Bool {Regex.swift:166 public func ~= (string: String, regex: Regex) -> Bool {String+ReplaceMatching.swift:18 public mutating func replaceFirstMatching(regex: Regex, with template: String) {String+ReplaceMatching.swift:50 replaceFirstMatching(Regex(pattern), with: template)String+ReplaceMatching.swift:72 public func replacingFirstMatching(regex: Regex, with template: String) -> String {String+ReplaceMatching.swift:99 return replacingFirstMatching(Regex(pattern), with: template)String+ReplaceMatching.swift:118 public mutating func replaceAllMatching(regex: Regex, with template: String) {String+ReplaceMatching.swift:150 replaceAllMatching(Regex(pattern), with: template)String+ReplaceMatching.swift:172 public func replacingAllMatching(regex: Regex, with template: String) -> String {String+ReplaceMatching.swift:199 return replacingAllMatching(Regex(pattern), with: template): NSRegularExpression 0006 0007 /// Create a `Regex` based on a pattern string. 0008 /// 0009 /// If `pattern` is not a valid regular expression, an error is thrown 0010 /// describing the failure. 0011 /// 0012 /// - parameters: 0013 /// - pattern: A pattern string describing the regex. 0014 /// - options: Configure regular expression matching options. 0015 /// For details, see `Regex.Options`. 0016 /// 0017 /// - throws: A value of `ErrorType` describing the invalid regular expression. 0018 public init(string pattern: String, options: Options = []) throws { 0019 regularExpression = try NSRegularExpression( 0020 pattern: pattern, 0021 options: options.toNSRegularExpressionOptions()) 0022 } 0023 0024 /// Create a `Regex` based on a static pattern string. 0025 /// 0026 /// Unlike `Regex.init(string:)` this initialiser is not failable. If `pattern` 0027 /// is an invalid regular expression, it is considered programmer error rather 0028 /// than a recoverable runtime error, so this initialiser instead raises a 0029 /// precondition failure. 0030 /// 0031 /// - requires: `pattern` is a valid regular expression. 0032 /// 0033 /// - parameters: 0034 /// - pattern: A pattern string describing the regex. 0035 /// - options: Configure regular expression matching options. 0036 /// For details, see `Regex.Options`. 0037 public init
Regex.swift:19 regularExpression = try NSRegularExpression(Regex.swift:39 regularExpression = try NSRegularExpression(Regex.swift:71 let match = regularExpressionRegex.swift:88 let matches = regularExpressionRegex.swift:123 return regularExpression.patternString+ReplaceMatching.swift:21 .regularExpressionString+ReplaceMatching.swift:121 .regularExpression(_ pattern: StaticString, options: Options = []) { 0038 do { 0039 regularExpression = try NSRegularExpression( 0040 pattern: pattern.stringValue, 0041 options: options.toNSRegularExpressionOptions()) 0042 } catch { 0043 preconditionFailure("unexpected error creating regex: \(error)") 0044 } 0045 } 0046 0047 // MARK: Matching 0048 0049 /// Returns `true` if the regex matches `string`, otherwise returns `false`. 0050 /// 0051 /// - parameter string: The string to test. 0052 /// 0053 /// - returns: `true` if the regular expression matches, otherwise `false`. 0054 /// 0055 /// - note: If the match is successful, `Regex.lastMatch` will be set with the 0056 /// result of the match. 0057 public func matches
String+ReplaceMatching.swift:50 replaceFirstMatching(Regex(pattern), with: template)String+ReplaceMatching.swift:99 return replacingFirstMatching(Regex(pattern), with: template)String+ReplaceMatching.swift:150 replaceAllMatching(Regex(pattern), with: template)String+ReplaceMatching.swift:199 return replacingAllMatching(Regex(pattern), with: template)(string: String) -> Bool { 0058 return match(string) != nil 0059 } 0060 0061 /// If the regex matches `string`, returns a `MatchResult` describing the 0062 /// first matched string and any captures. If there are no matches, returns 0063 /// `nil`. 0064 /// 0065 /// - parameter string: The string to match against. 0066 /// 0067 /// - returns: An optional `MatchResult` describing the first match, or `nil`. 0068 /// 0069 /// - note: If the match is successful, the result is also stored in `Regex.lastMatch`. 0070 public func match
Regex.swift:149 return regex.matches(string)Regex.swift:167 return regex.matches(string)(string: String) -> MatchResult? { 0071 let match = regularExpression 0072 .firstMatchInString(string, options: [], range: string.entireRange) 0073 .map { MatchResult(string, $0) } 0074 Regex._lastMatch = match 0075 return match 0076 } 0077 0078 /// If the regex matches `string`, returns an array of `MatchResult`, describing 0079 /// every match inside `string`. If there are no matches, returns an empty 0080 /// array. 0081 /// 0082 /// - parameter string: The string to match against. 0083 /// 0084 /// - returns: An array of `MatchResult` describing every match in `string`. 0085 /// 0086 /// - note: If there is at least one match, the first is stored in `Regex.lastMatch`. 0087 public func allMatches
Regex.swift:58 return match(string) != nilString+ReplaceMatching.swift:19 if let match = regex.match(self) {(string: String) -> [MatchResult] { 0088 let matches = regularExpression 0089 .matchesInString(string, options: [], range: string.entireRange) 0090 .map { MatchResult(string, $0) } 0091 if let firstMatch = matches.first { Regex._lastMatch = firstMatch } 0092 return matches 0093 } 0094 0095 // MARK: Accessing the last match 0096 0097 /// After any match, the result will be stored in this property for later use. 0098 /// This is useful when pattern matching: 0099 /// 0100 /// switch "hello" { 0101 /// case Regex("l+"): 0102 /// let count = Regex.lastMatch!.matchedString.characters.count 0103 /// print("matched \(count) characters") 0104 /// default: 0105 /// break 0106 /// } 0107 /// 0108 /// This property uses thread-local storage, and thus is thread safe. 0109 public static var lastMatch: MatchResult? { 0110 return _lastMatch 0111 } 0112 0113 private static let _lastMatchKey
String+ReplaceMatching.swift:119 for match in regex.allMatches(self).reverse() {= "me.sharplet.Regex.lastMatch" 0114 0115 private static var _lastMatch
Regex.swift:116 get { return ThreadLocal(_lastMatchKey).value }Regex.swift:117 set { ThreadLocal(_lastMatchKey).value = newValue }: MatchResult? { 0116 get { return ThreadLocal(_lastMatchKey).value } 0117 set { ThreadLocal(_lastMatchKey).value = newValue } 0118 } 0119 0120 // MARK: Describing 0121 0122 public var description
Regex.swift:74 Regex._lastMatch = matchRegex.swift:91 if let firstMatch = matches.first { Regex._lastMatch = firstMatch }Regex.swift:110 return _lastMatch: String { 0123 return regularExpression.pattern 0124 } 0125 0126 public var debugDescription: String { 0127 return "/\(description)/" 0128 } 0129 0130 } 0131 0132 // MARK: Pattern matching 0133 0134 /// Match `regex` on the left with some `string` on the right. Equivalent to 0135 /// `regex.matches(string)`, and allows for the use of a `Regex` in pattern 0136 /// matching contexts, e.g.: 0137 /// 0138 /// switch Regex("hello (\\w+)") { 0139 /// case "hello world": 0140 /// // successful match 0141 /// } 0142 /// 0143 /// - parameters: 0144 /// - regex: The regular expression to match against. 0145 /// - string: The string to test. 0146 /// 0147 /// - returns: `true` if the regular expression matches, otherwise `false`. 0148 public func ~= (regex: Regex, string: String) -> Bool { 0149 return regex.matches(string) 0150 } 0151 0152 /// Match `string` on the left with some `regex` on the right. Equivalent to 0153 /// `regex.matches(string)`, and allows for the use of a `Regex` in pattern 0154 /// matching contexts, e.g.: 0155 /// 0156 /// switch "hello world" { 0157 /// case Regex("hello (\\w+)"): 0158 /// // successful match 0159 /// } 0160 /// 0161 /// - parameters: 0162 /// - regex: The regular expression to match against. 0163 /// - string: The string to test. 0164 /// 0165 /// - returns: `true` if the regular expression matches, otherwise `false`. 0166 public func ~= (string: String, regex: Regex) -> Bool { 0167 return regex.matches(string) 0168 } 0169 0170 import Foundation 0171
Regex.swift:127 return "/\(description)/"