Draft: Regular Expression in Swift

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

3 Likes

Hi Joshua,

while this is a great idea, I'm afraid that this needs to be more thought-through. For example, which regex standards would you like to include? I really hope that Swift's regex support will not get stuck with plain ICU regexes like NSRegularExpression. I personally would love to see Perl-syntax improvements such as named groups (any many others):

let regexString = "NS_ENUM\((?P<TYPE>\w+), (?P<NAME>\w+)\)"

let mySource = "NS_ENUM(NSInteger, NSComparisonResult)"
mySource.match("TYPE", in: regexString) // NSInteger

Of course, it would be great if the compiler support could be extended so that the TYPE and NAME variables can be extracted without using string literals, e.g. Regex(regexString).firstMatch(in: mySource)?.TYPE

I have written myself an ObjC wrapper arround C++ implementation of such regex (re2) and use that from Swift instead of using NSRegularExpression.

Regarding your snippet:

- it's always good to keep in mind that in many usecases you only need the first match, not all of them
- instead of search(_:), consider matches(_:slight_smile:
- huge part of regex power is in replacing occurrences, which is missing

···

On Aug 10, 2017, at 8:25 AM, Joshua Alvarado via swift-evolution <swift-evolution@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@gmail.com <mailto:alvaradojoshua0@gmail.com>

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

Hi Joshua,

Thanks for bringing this topic up. It may help to outline why regular expressions were deferred until Swift 5. The work to create a regular expression type itself, and even to add a regex literal protocol, is fairly straightforward and probably could have been done in the Swift 4 timeframe (maybe by making NSRegularExpression a value type and refreshing its API), but there are other design aspects that we need to explore prior to that, many of which have compiler impact, or would need to factor in the underlying representation of string in order to be efficient, in order to make Swift-native regular expressions really great.

Right now, the top priority for Swift 5 is ABI stability, and String plays a fairly large part in that. We need to finalize the size of String (currently 3 words, but 2 is the likely final size), implement the small string optimization, and decide which parts of String need to be fragile/inlineable and which need to be resilient.

Since ABI stability takes priority, this give us time in the mean-time to consider the broader design questions of what Swift-native regular expressions would look like. These design considerations probably need to come ahead of designing an API for specific types like a regex, matches etc.

Some examples of these kind of questions include:

What syntax changes to the usual form of regexes should be considered? For example, what should ‚Äú.‚ÄĚ mean in regular expressions? It would be out of keeping for it to mean a code unit, when applied to Swift.String. Alternatively, should regular expressions work on all the views? In which case, ‚Äú.‚ÄĚ could mean a grapheme when applied to String, but a code unit when applied to the UTF16 view.

How can let bindings work on capture groups i.e. rather than having named capture groups like in Perl, can we bind directly to variables in switches etc? Could types conform to a RegularExpressionCapturable that would consume part of the string and initialize self, so that you could capture not just Substring but any type? You can’t express this in the language today, and would need compiler integration. This integration could start more hard-coded in order to deliver value in the Swift 5 release, but hopefully be generalizable in later releases.

What other std lib APIs should be changed once we have regular expressions? For example, split ought to work with regexes e.g. let words = sentence.split(separator: /\w+/). How can this generalize to Collections where possible? E.g. [1,2,3,4].index(of: [2,3]) ought to work just as ‚Äúabcd‚ÄĚ.index(of: /bc/) should.

···

On Aug 10, 2017, at 7:24 AM, Joshua Alvarado via swift-evolution <swift-evolution@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@gmail.com

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

3 Likes

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@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@gmail.com <mailto:alvaradojoshua0@gmail.com>

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

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@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@swift.org <mailto:swift-evolution@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@gmail.com <mailto:alvaradojoshua0@gmail.com>

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

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

Thank you for bringing this up! Swift String has a lot of expressivity gaps we’re trying to tackle in Swift 5. I think that both language and library support for flexible matching and transformation is needed, likely through a regex-like construct. Libraries and prototypes like this help drive the discussion. Do you have a package that implements this?

Ben’s email highlights a lot of the design considerations that need to be thought through. We’ll likely be iterating over designs on swift-evolution for the various components, but we also need a high level vision for this. I’m working on a new version of the String Manifesto that incorporates lessons learned from the implementation of Swift 4 while fleshing out a vision for Swift 5 and beyond.

Part of that vision includes a declarative way for programmers to express string processing and transformation cleanly in Swift. Perl 6 is the gold standard for string processing in programming languages. Any insights shown in Perl 6’s design[1] is likely to be beneficial for Swift. I don’t think we should just cargo-cult its constructs verbatim, but our eventual design is likely to be influenced by it. Swift’s tendency for clarity over terseness means that these concepts might look different on the surface, and Swift’s type system is very different from Perl 6’s, but we should certainly steal the best ideas. Good languages copy; great languages steal!

This design discussion will likely unfold over the coming months. In the mean time, I’m interested in playing with your approach and the kinds of problems it solves cleanly and effectively. Could you link to an SPM package implementing this? Do you have example code using it?

[1] https://docs.perl6.org/language/regexes

···

On Aug 10, 2017, at 2:58 AM, Ben Cohen via swift-evolution <swift-evolution@swift.org> wrote:

Hi Joshua,

Thanks for bringing this topic up. It may help to outline why regular expressions were deferred until Swift 5. The work to create a regular expression type itself, and even to add a regex literal protocol, is fairly straightforward and probably could have been done in the Swift 4 timeframe (maybe by making NSRegularExpression a value type and refreshing its API), but there are other design aspects that we need to explore prior to that, many of which have compiler impact, or would need to factor in the underlying representation of string in order to be efficient, in order to make Swift-native regular expressions really great.

Right now, the top priority for Swift 5 is ABI stability, and String plays a fairly large part in that. We need to finalize the size of String (currently 3 words, but 2 is the likely final size), implement the small string optimization, and decide which parts of String need to be fragile/inlineable and which need to be resilient.

Since ABI stability takes priority, this give us time in the mean-time to consider the broader design questions of what Swift-native regular expressions would look like. These design considerations probably need to come ahead of designing an API for specific types like a regex, matches etc.

Some examples of these kind of questions include:

What syntax changes to the usual form of regexes should be considered? For example, what should ‚Äú.‚ÄĚ mean in regular expressions? It would be out of keeping for it to mean a code unit, when applied to Swift.String. Alternatively, should regular expressions work on all the views? In which case, ‚Äú.‚ÄĚ could mean a grapheme when applied to String, but a code unit when applied to the UTF16 view.

How can let bindings work on capture groups i.e. rather than having named capture groups like in Perl, can we bind directly to variables in switches etc? Could types conform to a RegularExpressionCapturable that would consume part of the string and initialize self, so that you could capture not just Substring but any type? You can’t express this in the language today, and would need compiler integration. This integration could start more hard-coded in order to deliver value in the Swift 5 release, but hopefully be generalizable in later releases.

What other std lib APIs should be changed once we have regular expressions? For example, split ought to work with regexes e.g. let words = sentence.split(separator: /\w+/). How can this generalize to Collections where possible? E.g. [1,2,3,4].index(of: [2,3]) ought to work just as ‚Äúabcd‚ÄĚ.index(of: /bc/) should.

On Aug 10, 2017, at 7:24 AM, Joshua Alvarado via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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@gmail.com <mailto:alvaradojoshua0@gmail.com>

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

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

1 Like

I guess everyone agrees that there should be support for regular expressions - I'm just not sure where, how and when to integrate it…
People seem to have a (sometimes unhealthy…) passion for regex, so I'd like to encourage a critical look on their importance.
For example, I don't think they belong into the stdlib ‚ÄĒ there are more basic (and much more simple) things like a Result-type or an operator for function concatenation which are not included, so why should we treat regular expressions differently?
But It doesn't look like we'll see a fitting library that's bundled with Swift anytime soon, and third-party regex Frameworks imho would cause big confusion.

Anyways, as you suggest a special syntax, it would be required to integrate support into Swift itself… although this is even deeper than the stdlib, I'm more positive towards that direction, as it would allow some neat tricks:
The expressions could not only be checked, but also translated at compile time ‚ÄĒ which could give Swift a real kickstart to beat established solutions in terms of execution speed.
It would, on the other hand, not work for non-literal strings, so regular expressions that are created at runtime would still need extra support…

If Core would consider to include compile-time regular expressions in the future, I'd support adding the new syntax, even if it's only used like NSExpression in the first implementation. Without that option, I think it's not worth it, and I'd rather try to use custom operators to build matchers that don't follow any regex standard, but are fast and safe to use.

I guess everyone agrees that there should be support for regular expressions - I'm just not sure where, how and when to integrate it…
People seem to have a (sometimes unhealthy…) passion for regex, so I'd like to encourage a critical look on their importance.
For example, I don't think they belong into the stdlib ‚ÄĒ there are more basic (and much more simple) things like a Result-type or an operator for function concatenation which are not included, so why should we treat regular expressions differently?
But It doesn't look like we'll see a fitting library that's bundled with Swift anytime soon, and third-party regex Frameworks imho would cause big confusion.

This goes back to the idea of ‚Äúnon-standard libraries‚ÄĚ that ship with Swift, but aren‚Äôt imported by default. I think a ‚ÄúSwift.Regex‚ÄĚ module would be awesome, but I agree that it probably doesn‚Äôt need to be in the standard library.

Dave

···

On Aug 10, 2017, at 11:48 AM, Tino Heth via swift-evolution <swift-evolution@swift.org> wrote:

Anyways, as you suggest a special syntax, it would be required to integrate support into Swift itself… although this is even deeper than the stdlib, I'm more positive towards that direction, as it would allow some neat tricks:
The expressions could not only be checked, but also translated at compile time ‚ÄĒ which could give Swift a real kickstart to beat established solutions in terms of execution speed.
It would, on the other hand, not work for non-literal strings, so regular expressions that are created at runtime would still need extra support…

If Core would consider to include compile-time regular expressions in the future, I'd support adding the new syntax, even if it's only used like NSExpression in the first implementation. Without that option, I think it's not worth it, and I'd rather try to use custom operators to build matchers that don't follow any regex standard, but are fast and safe to use.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

To me, regexes definitely feel like they belong in Foundation. Also, I don’t think they’d be well-served by a literal syntax (even just ExpressibleByStringLiteral) because this privileges using the default regex options over specifying non-default options.

The compilation issue seems a bit tricky, especially if Regex is a value type. To avoid throwing away a cached compiled version of the Regex, you’d need to remember to compile it before passing it to a function. Perhaps the initializers could have a `precompile` argument so that you’d never forget to compile the regex. The Swift compiler could then optimize around that where possible. It should definitely be possible to create uncompiled regexes, though. I also agree that the compiler should warn about invalid regexes where possible.

···

On Aug 10, 2017, at 2:30 PM, Dave DeLong via swift-evolution <swift-evolution@swift.org> wrote:

On Aug 10, 2017, at 11:48 AM, Tino Heth via swift-evolution <swift-evolution@swift.org> wrote:

I guess everyone agrees that there should be support for regular expressions - I'm just not sure where, how and when to integrate it…
People seem to have a (sometimes unhealthy…) passion for regex, so I'd like to encourage a critical look on their importance.
For example, I don't think they belong into the stdlib ‚ÄĒ there are more basic (and much more simple) things like a Result-type or an operator for function concatenation which are not included, so why should we treat regular expressions differently?
But It doesn't look like we'll see a fitting library that's bundled with Swift anytime soon, and third-party regex Frameworks imho would cause big confusion.

This goes back to the idea of ‚Äúnon-standard libraries‚ÄĚ that ship with Swift, but aren‚Äôt imported by default. I think a ‚ÄúSwift.Regex‚ÄĚ module would be awesome, but I agree that it probably doesn‚Äôt need to be in the standard library.

Dave

Anyways, as you suggest a special syntax, it would be required to integrate support into Swift itself… although this is even deeper than the stdlib, I'm more positive towards that direction, as it would allow some neat tricks:
The expressions could not only be checked, but also translated at compile time ‚ÄĒ which could give Swift a real kickstart to beat established solutions in terms of execution speed.
It would, on the other hand, not work for non-literal strings, so regular expressions that are created at runtime would still need extra support…

If Core would consider to include compile-time regular expressions in the future, I'd support adding the new syntax, even if it's only used like NSExpression in the first implementation. Without that option, I think it's not worth it, and I'd rather try to use custom operators to build matchers that don't follow any regex standard, but are fast and safe to use.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

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

Regexes somehow don't seem to have got a lot of attention over the last years, which is a shame. It crossed my mind recently that you could do some exciting static things with regexes, so I looked through the forums for the most recent regex proposal and this seems to be it.

I would love to have an additional protocol built into Swift: ExpressibleByRegex. This will inherit from ExpressibleByStringLiteral and have a static property pattern : StaticRegex (static regexes being for regexes what static strings are for strings). You could have types like URLs conform to that, and if you write code like

let url : URL = ...

you will get an error at compile time, if the right hand side doesn't match the regex. I suppose this could even be solved by a linter?

1 Like