<div dir="ltr">Hey guys,<div><br></div><div>TL;DR; proposed solution in the second half.</div><div><br></div><div>actually, we are already doing something like this without modifying the language itself here: <a href="https://github.com/crossroadlabs/Regex/blob/swift2.x/Regex/String%2BRegex.swift">https://github.com/crossroadlabs/Regex/blob/swift2.x/Regex/String%2BRegex.swift</a></div><div><br></div><div>It's Scala style (.r notion). Works perfectly with switch keyword:</div><div>
<p class=""><i><span class="">switch</span><span class=""> letter {</span></i></p>
<p class=""><i><span class=""> </span><span class="">//note .r after the string literal of the pattern</span></i></p>
<p class=""><i><span class=""> </span><span class="">case</span><span class=""> </span><span class="">"\\d"</span><span class="">.</span><span class="">r: </span><span class="">print</span><span class="">(</span><span class="">"digit"</span><span class="">)</span></i></p>
<p class=""><i><span class=""> </span><span class="">case</span><span class=""> </span><span class="">"[a-z]"</span><span class="">.</span><span class="">r: </span><span class="">print</span><span class="">(</span><span class="">"letter"</span><span class="">)</span></i></p>
<p class=""><i><span class=""> </span><span class="">default</span><span class="">: </span><span class="">print</span><span class="">(</span><span class="">"bizarre symbol"</span><span class="">)</span></i></p>
<p class=""><span class=""><i>}</i></span></p><p class="">You can try it in release 0.8 (for Swift 2.x, Swift 3.0 release with this feature is not issued yet).</p><p class="">What we really lack though is matching objects to tuples with <i>switch</i>. Here is the explanation:</p><p class="">Let's say we have a match:</p><p class="">let match = "(\d*)(.*)".r.findFirst("123abc")</p><p class="">what I would love to do with it is the following:</p><p class=""><i>switch match {</i></p><p class=""><i>case (_, "xyz"): print("end of alphabet")</i></p><p class=""><i>case ("123", _): print("beginning of numerics")</i></p><p class=""><i>default: print("I don't care")</i></p><p class=""><i>}</i></p><p class="">It would be possible if ~= operator was a bit more powerful and allow to match against tuples (currently, it can't even without underscores). What I would like to propose (will create an evolution proposal later if it's something the core-team is willing to consider) is to:</p><p class="">1. Either extend ~= to allow to match tuples. Currently, I see it the following way (an example):</p><p class=""><i>// you have to create an operator for each tuple size, any ideas on a better solution?</i></p><p class=""><i>func ~=<A, B>(match:Match, tuple:(A?, B?)) -> Bool</i></p><p class="">Why optionals? Well, this way we could process _ substitutions.</p><p class="">2. Implement Scala-like apply/unapply. Good too, but option one sounds more "Swiftish" to me personally.</p><p class=""><br></p><p class="">Why is this cool? Well, this is not just about Regular Expressions, it's rather overall language power and Regex is a nice example. I think there will be a lot of examples, especially with the web apps. Some additional ones I can think of right away is processing of URL or query params or "parsing" of message object (i.e. in Actors or Queues).</p><p class="">Why making the match object to be an enum would not work? Well, first of all there is some more additional info in it which I would like to keep hidden for the user (i.e. full match, etc.) and not to push to match to. This is not just one use case and in others it might become even more important.</p><p class="">Hope it's clear and understandable (let me know if not and I'll bring it in more details). Would this fit to Swift 3.x (no existing code impact) or to Swift 4.x?</p><p class="">Best,</p><p class="">Daniel</p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 30, 2016 at 12:37 PM, Charlie Monroe via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">> Foundation already has NSRegularExpression. Do you mean that the<br>
> stdlib could potentially duplicate Foundation functionality?<br>
<br>
</span>NSRegularExpression is not really easy to use for most common usecases (first match in string, etc.) + it lacks a lot of features e.g. Python has (named groups, etc.). I've personally never used NSRegularExpression and rather wrote an ObjC wrapper around re2 (<a href="https://github.com/google/re2/" rel="noreferrer" target="_blank">https://github.com/google/re2/</a>).<br>
<br>
I'd really like Swift's regex to be much more powerful and be able to match against it in a switch:<br>
<br>
switch someString {<br>
case /\d+:<br>
...<br>
case /w+:<br>
...<br>
...<br>
<br>
Also, you can have compile-time check whether the expression is valid if regex is part of the language. But that's kind of getting too specific and away from the original discussion.<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
> If so,<br>
> what are the implications for Foundation (and<br>
> swift-corelibs-foundation)? Does this also mean that other "stringy"<br>
> functionality could arrive in the stdlib, for example a Swifty JSON<br>
> serializer/deserializer?<br>
><br>
> Best wishes,<br>
><br>
> --<br>
> Ian Partridge<br>
> _______________________________________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div>