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

Initializers

  • Create a Regex from a static pattern string and options.

    Tip: Wrap the pattern string in # to reduce the need for escaping. For example: #"\d+"#.

    Supported regex syntax.

    Declaration

    Swift

    public init(
    	_ pattern: StaticString,
    	options: Options = [],
    	file: StaticString = #fileID,
    	line: Int = #line
    )
  • Create a Regex from a pattern string and options.

    Tip: Wrap the pattern string in # to reduce the need for escaping. For example: #"\d+"#.

    Supported regex syntax.

    Declaration

    Swift

    @_disfavoredOverload
    public init(
    	_ pattern: String,
    	options: Options = []
    ) throws
  • Create a Regex from a NSRegularExpression.

    Declaration

    Swift

    @_disfavoredOverload
    public init(_ regularExpression: NSRegularExpression)

Methods

  • Returns whether there is a match in the given string.

    import Regex
    
    Regex(#"^\d+$"#).isMatched(by: "123")
    //=> true
    

    Declaration

    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]

Properties

  • The regular expression pattern.

    Declaration

    Swift

    public var pattern: String { get }
  • The regular expression options.

    Declaration

    Swift

    public var options: Options { get }

Types

  • Undocumented

    Declaration

    Swift

    public typealias Options = NSRegularExpression.Options
  • Undocumented

    Declaration

    Swift

    public typealias MatchingOptions = NSRegularExpression.MatchingOptions
  • A regex match.

    See more

    Declaration

    Swift

    public struct Match : Hashable

Operators

  • 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

Helpers

  • 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