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
alvaradojosh...@gmail.com

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to