[swift-evolution] Draft: Regular Expression in Swift

Joshua Alvarado alvaradojoshua0 at gmail.com
Thu Aug 10 01:24:41 CDT 2017


Hey everyone,

I would like to pitch an implementation of Regex in Swift and gather all of your thoughts.

Motivation:
In the String Manifesto for Swift 4, addressing regular expressions was not in scope. Swift 5 would be a more fitting version to address the implementation of Regex in Swift. NSRegularExpression is a suitable solution for pattern matching but the API is in unfitting for the future direction of Swift.

Implementation:
The Regular expression API will be implemented by a Regex structure object which is a regular expression that you can apply to Unicode strings. The Regex struct will conform to the RegexProtocol, which is a type that can represent a regular expression. ExpressibleByRegexLiteral will be used to initialize a regex literal creating an easy to use syntax and a Match structure will be used to represent a match found with a Regex.

Draft of implementation:

protocol ExpressibleByRegexLiteral {
    associatedtype RegexLiteralType

    init(regexLiteral value: Self.RegexLiteralType)
}

// Structure of information about a match of regex on a string
struct Match {
    var regex: Regex
    var start: String.Index
    var end: String.Index
}

protocol RegexProtocol {
    init(pattern: String) throws

    var pattern: String { get } // string representation of the pattern
    func search(string: String) -> Bool // used to check if a match is found at all in the string
    func match(string: String) -> [Match] // returns an array of all the matches
    func match(string: String, using: ((Match) -> Void)) // enmuerate over matches
}

struct Regex: RegexProtocol {
    init(pattern: Regex, options: Regex.Options)
    let options: [Regex.Options]
    static let word: Regex // \w
    // other useful regexes can be added as well
}

// Examples

let regex = \[a-zA-Z]+\
let matches = regex.match("Matching words in text.")

for match in matches {
    print("Found a match at in string at \(match.start) to \(match.end)")
}

let helloStr = "Hello world"

Regex.word.match(helloStr) { match in
    print("Matched \(helloStr[match.start..<match.end])")
}
 
Of course this is a scratch implementation I made but it is to open discussion on the topic. I feel the Regex struct itself will need more methods and variables such as for flags and number of groups. Please provide feedback with improvements to the code, concerns on the topic, or just open up discussion. Thank you!

Joshua Alvarado
alvaradojoshua0 at gmail.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170809/57ee7288/attachment.html>


More information about the swift-evolution mailing list