[Draft] Swift implementation Regex added


(Joshua Alvarado) #1

My first proposal so take it easy on me :slight_smile: This is a draft so feedback is very welcome and any help. Thank you swiftys.

*Introduction*

Swift doesn鈥檛 have a native implementation of Regex which is included in many other languages. This proposal is a suggestion to introduce a native Regex type into Swift.

*Motivation*
Working with NSRegularExpression is not very swift like and it is lacking in ease of use. More motivation comes from also comes from one of the goals of swift with Swiftification of imported Objective-C APIs.

*Proposed solution*

Add a struct of Regex that can be used with the String type.

Regex can be initialized with a String pattern of the Regex and have simple properties such as accessing the pattern and toString of the pattern.

String to be extended for Regex support. Functions to add be:

mutating func replace(regex: Regex, withString: String)
search(regex: Regex) -> Character.Index
match(regex: Regex) -> Match? // Match is another new type

Match is a new type that can give more information on a regex match. Match will have properties such as numberOfMatches, components (the substring of matches in an array), and rangeOfMatch.

*Impact on existing code*
NSRegularExpression will stay and can still be used in combination, but a more swift like implementation will be out of the box in Swift.

*Alternatives considered*
Regex could be a protocol added to String for matching.


(Brandon Knope) #2

There has been talk on this list of adding a fully featured RegEx feature. It has been pushed back to a later release as the window for Swift 3 is closing

Brandon

路路路

On Jul 25, 2016, at 12:24 PM, Joshua Alvarado via swift-evolution <swift-evolution@swift.org> wrote:

My first proposal so take it easy on me :slight_smile: This is a draft so feedback is very welcome and any help. Thank you swiftys.

*Introduction*

Swift doesn鈥檛 have a native implementation of Regex which is included in many other languages. This proposal is a suggestion to introduce a native Regex type into Swift.

*Motivation*
Working with NSRegularExpression is not very swift like and it is lacking in ease of use. More motivation comes from also comes from one of the goals of swift with Swiftification of imported Objective-C APIs.

*Proposed solution*

Add a struct of Regex that can be used with the String type.

Regex can be initialized with a String pattern of the Regex and have simple properties such as accessing the pattern and toString of the pattern.

String to be extended for Regex support. Functions to add be:

mutating func replace(regex: Regex, withString: String)
search(regex: Regex) -> Character.Index
match(regex: Regex) -> Match? // Match is another new type

Match is a new type that can give more information on a regex match. Match will have properties such as numberOfMatches, components (the substring of matches in an array), and rangeOfMatch.

*Impact on existing code*
NSRegularExpression will stay and can still be used in combination, but a more swift like implementation will be out of the box in Swift.

*Alternatives considered*
Regex could be a protocol added to String for matching.

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


(Will Field-Thompson) #3

The date for Swift 4 proposals is August 1 (
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160711/024424.html),
so you might want to try posting this again then.

路路路

On Mon, Jul 25, 2016 at 12:55 PM Brandon Knope via swift-evolution < swift-evolution@swift.org> wrote:

There has been talk on this list of adding a fully featured RegEx feature.
It has been pushed back to a later release as the window for Swift 3 is
closing

Brandon

On Jul 25, 2016, at 12:24 PM, Joshua Alvarado via swift-evolution < > swift-evolution@swift.org> wrote:

My first proposal so take it easy on me :slight_smile: This is a draft so feedback is very welcome and any help. Thank you swiftys.

*Introduction*

Swift doesn鈥檛 have a native implementation of Regex which is included in many other languages. This proposal is a suggestion to introduce a native Regex type into Swift.

*Motivation*
Working with NSRegularExpression is not very swift like and it is lacking in ease of use. More motivation comes from also comes from one of the goals of swift with Swiftification of imported Objective-C APIs.

*Proposed solution*

Add a struct of Regex that can be used with the String type.

Regex can be initialized with a String pattern of the Regex and have simple properties such as accessing the pattern and toString of the pattern.

String to be extended for Regex support. Functions to add be:

mutating func replace(regex: Regex, withString: String)
search(regex: Regex) -> Character.Index
match(regex: Regex) -> Match? // Match is another new type

Match is a new type that can give more information on a regex match. Match will have properties such as numberOfMatches, components (the substring of matches in an array), and rangeOfMatch.

*Impact on existing code*
NSRegularExpression will stay and can still be used in combination, but a more swift like implementation will be out of the box in Swift.

*Alternatives considered*
Regex could be a protocol added to String for matching.

_______________________________________________
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


(Joshua Alvarado) #4

Okay thanks for taking a look I will have a better implementation ready for August 1st.

Alvarado, Joshua

路路路

On Jul 25, 2016, at 11:14 AM, Will Field-Thompson <will.a.ft@gmail.com> wrote:

The date for Swift 4 proposals is August 1 (https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160711/024424.html), so you might want to try posting this again then.

On Mon, Jul 25, 2016 at 12:55 PM Brandon Knope via swift-evolution <swift-evolution@swift.org> wrote:
There has been talk on this list of adding a fully featured RegEx feature. It has been pushed back to a later release as the window for Swift 3 is closing

Brandon

On Jul 25, 2016, at 12:24 PM, Joshua Alvarado via swift-evolution <swift-evolution@swift.org> wrote:

My first proposal so take it easy on me :slight_smile: This is a draft so feedback is very welcome and any help. Thank you swiftys.

*Introduction*

Swift doesn鈥檛 have a native implementation of Regex which is included in many other languages. This proposal is a suggestion to introduce a native Regex type into Swift.

*Motivation*
Working with NSRegularExpression is not very swift like and it is lacking in ease of use. More motivation comes from also comes from one of the goals of swift with Swiftification of imported Objective-C APIs.

*Proposed solution*

Add a struct of Regex that can be used with the String type.

Regex can be initialized with a String pattern of the Regex and have simple properties such as accessing the pattern and toString of the pattern.

String to be extended for Regex support. Functions to add be:

mutating func replace(regex: Regex, withString: String)
search(regex: Regex) -> Character.Index
match(regex: Regex) -> Match? // Match is another new type

Match is a new type that can give more information on a regex match. Match will have properties such as numberOfMatches, components (the substring of matches in an array), and rangeOfMatch.

*Impact on existing code*
NSRegularExpression will stay and can still be used in combination, but a more swift like implementation will be out of the box in Swift.

*Alternatives considered*
Regex could be a protocol added to String for matching.

_______________________________________________
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


(Brent Royal-Gordon) #5

Something to keep in mind when you write your proposal: At least one member of the core team has expressed a desire for regular expressions to be deeply integrated into the language, with strong connections to `case` syntax and destructuring, and a Perl 6-style rethink of the syntax. This would be a much larger design than just Swift-ifying NSRegularExpression, of course.

Another route you might take is to conform the existing NSRegularExpression to the `ExpressibleByStringLiteral` protocol and then reopen the issue of alternate string literal syntaxes. String literals could be enhanced so that it's easier to write one containing backslashes and other special characters used in regular expressions. With a few other enhancements, like regex matching operators, we could get a pretty natural syntax going. Previous work in this area: <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160418/015500.html>

路路路

On Jul 25, 2016, at 11:40 AM, Joshua Alvarado via swift-evolution <swift-evolution@swift.org> wrote:

Okay thanks for taking a look I will have a better implementation ready for August 1st.

--
Brent Royal-Gordon
Architechies


(Garth Snyder) #6

Brent Royal-Gordon: Something to keep in mind when you write your proposal: At least one member of the core team has expressed a desire for regular expressions to be deeply integrated into the language, with strong connections to `case` syntax and destructuring, and a Perl 6-style rethink of the syntax.

I鈥檇 love to see this as well. I鈥檓 glad it has some support on the inside!

Garth