[swift-evolution] Empower String type with regular expression

Brent Royal-Gordon brent at architechies.com
Tue Dec 8 16:41:52 CST 2015


> 1. The semantics of the =~ operator in other languages maps to a return value of Int? - matching position or nil with no match. This means you would likely need to write if str =~ foo != nil { … }

Actually, I suspect the return value would be more like Range<String.Character.Index>?, or maybe even Regex.Match?. 

> 2. The semantics of the =~ operator produce state around matched groups, which is usually exposed to the language as either thread-local or block-local data.

This is hugely dependent on the language and I think it’s pretty obvious that Swift would take a return-value-based approach:

	if let match = string =~ /”([^\\”]*(\\[\\”][^\\”]*)*)”/ {
		let quotedString = match.substrings[1]
		…
	}

> For regex literals, my only concern is that it makes regex a language feature over an extended or standard library feature.

Not necessarily. The only thing that *has* to be in the language is support for parsing regex literals, and the only thing that has to be in the standard library is RegexLiteralConvertible. Even if there is a Regex type in the standard library, just that type could be dropped there without requiring huge compiler changes.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list