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(_:) - 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 > alvaradojosh...@gmail.com <mailto:alvaradojosh...@gmail.com> > > _______________________________________________ > 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