<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&#39;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="">&quot;\\d&quot;</span><span class="">.</span><span class="">r: </span><span class="">print</span><span class="">(</span><span class="">&quot;digit&quot;</span><span class="">)</span></i></p>
<p class=""><i><span class="">    </span><span class="">case</span><span class=""> </span><span class="">&quot;[a-z]&quot;</span><span class="">.</span><span class="">r: </span><span class="">print</span><span class="">(</span><span class="">&quot;letter&quot;</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="">&quot;bizarre symbol&quot;</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&#39;s say we have a match:</p><p class="">let match = &quot;(\d*)(.*)&quot;.r.findFirst(&quot;123abc&quot;)</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 (_, &quot;xyz&quot;): print(&quot;end of alphabet&quot;)</i></p><p class=""><i>case (&quot;123&quot;, _): print(&quot;beginning of numerics&quot;)</i></p><p class=""><i>default: print(&quot;I don&#39;t care&quot;)</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&#39;t even without underscores). What I would like to propose (will create an evolution proposal later if it&#39;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 ~=&lt;A, B&gt;(match:Match, tuple:(A?, B?)) -&gt; 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 &quot;Swiftish&quot; to me personally.</p><p class=""><br></p><p class="">Why is this cool? Well, this is not just about Regular Expressions, it&#39;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 &quot;parsing&quot; 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&#39;s clear and understandable (let me know if not and I&#39;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">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">&gt; Foundation already has NSRegularExpression. Do you mean that the<br>
&gt; 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&#39;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&#39;d really like Swift&#39;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&#39;s kind of getting too specific and away from the original discussion.<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
&gt; If so,<br>
&gt; what are the implications for Foundation (and<br>
&gt; swift-corelibs-foundation)? Does this also mean that other &quot;stringy&quot;<br>
&gt; functionality could arrive in the stdlib, for example a Swifty JSON<br>
&gt; serializer/deserializer?<br>
&gt;<br>
&gt; Best wishes,<br>
&gt;<br>
&gt; --<br>
&gt; Ian Partridge<br>
&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt; <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>