[swift-evolution] Draft: Regular Expression in Swift

Jacob Williams ponyboy47 at gmail.com
Thu Aug 10 10:47:42 CDT 2017


Disclaimer: I am not well versed in the various complexities of regex implementations.

That being said, I would very much like to see better regex support in Swift. Preferably one that is easier to pick up than the NSRegularExpression of ObjC and possibly as easy to start using as python or ruby.

Just some things to consider for the implementation:

- Plain old searching for a match
- Multiple (named) capture groups
	- I liked the suggestion of binding straight to variables (perhaps with closures)
- Replacements
- I’m also not a fan of using the backslash for instantiating a regex literal. Many other languages use the forward slash (Off the top of my head all I can think of is Ruby, but I’m sure there are others)


> On Aug 10, 2017, at 4:28 AM, Omar Charif via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi Joshua,
> 
> I also feel a huge gap when it comes to string matching, whether it is implementation or performance.
> I also wrote a library for String matching called StringMap and it has been way for performant than regular expressions.
> I recently proposed to include it in core foundation but it seems that I was a bit late and the team was busy finishing their unimplemented functions so they advised me to ship the code in SPM instead.
> So I am also willing to work in this issue.
> 
> 
> 
> 
>> On Aug 10, 2017, at 8:25 AM, Joshua Alvarado via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> 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 <mailto:alvaradojoshua0 at gmail.com>
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170810/9768cba9/attachment.html>


More information about the swift-evolution mailing list