Regex
public struct Regex : Hashable
Create a regular expression from a pattern string and options.
import Regex
let regex = Regex(#"^[a-z]+$"#, options: .caseInsensitive)
regex.isMatched(by: "Unicorn")
//=> true
-
Create a
Regexfrom a static pattern string and options.Tip: Wrap the pattern string in
#to reduce the need for escaping. For example:#"\d+"#.Declaration
Swift
public init( _ pattern: StaticString, options: Options = [], file: StaticString = #fileID, line: Int = #line ) -
Create a
Regexfrom a pattern string and options.Tip: Wrap the pattern string in
#to reduce the need for escaping. For example:#"\d+"#.Declaration
Swift
@_disfavoredOverload public init( _ pattern: String, options: Options = [] ) throws -
Create a
Regexfrom aNSRegularExpression.Declaration
Swift
@_disfavoredOverload public init(_ regularExpression: NSRegularExpression)
-
Returns whether there is a match in the given string.
import Regex Regex(#"^\d+$"#).isMatched(by: "123") //=> trueDeclaration
Swift
public func isMatched(by string: String) -> Bool -
Returns the first match in the given string.
import Regex Regex(#"\d+"#).firstMatch(in: "123-456")?.value //=> "123"Declaration
Swift
public func firstMatch(in string: String) -> Match? -
Returns all the matches in the given string.
import Regex Regex(#"\d+"#).allMatches(in: "123-456").map(\.value) //=> ["123", "456"]Declaration
Swift
public func allMatches(in string: String) -> [Match]
-
The regular expression pattern.
Declaration
Swift
public var pattern: String { get } -
The regular expression options.
Declaration
Swift
public var options: Options { get }
-
Undocumented
Declaration
Swift
public typealias Options = NSRegularExpression.Options -
Undocumented
Declaration
Swift
public typealias MatchingOptions = NSRegularExpression.MatchingOptions -
A regex match.
See moreDeclaration
Swift
public struct Match : Hashable
-
Enables using a regex for pattern matching.
import Regex switch "foo123" { case Regex(#"^foo\d+$"#): print("Match!") default: break }Declaration
Swift
public static func ~= (string: String, regex: `Self`) -> Bool -
Enables using a regex for pattern matching.
import Regex switch Regex(#"^foo\d+$"#) { case "foo123": print("Match!") default: break }Declaration
Swift
public static func ~= (regex: `Self`, string: String) -> Bool
-
Returns a string by adding backslash escapes as necessary to protect any characters that would match as pattern metacharacters.
Declaration
Swift
public static func escapingPattern(for string: String) -> String
View on GitHub
Install in Dash
Regex Structure Reference