<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Well, this hasn’t exactly gone in the direction I was expecting. Are raw literals off the table?<div class=""><br class=""></div><div class="">I have plenty of opinions about how to integrate Regex though. It’s not the literal that’s</div><div class="">the problem but the operators that surround it and these operators need to allow you to:</div><div class="">check for a match, get the match or groups, replace matches, iterate over matches etc.</div><div class=""><br class=""></div><div class="">One notion I explored was the idea that in the same way an index or key of a collection</div><div class="">refers to a subset of the data, subscripting using a string (regex) on a string refers to</div><div class="">a subset of the string, It's also atomic so you don’t have to worry about precedence.</div><div class=""><br class=""></div><div class="">Bear with me, It works out better than you might expect once you get used to the idea.</div><div class="">It’s certainly very succinct for common cases.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">var</span><span style="color: #000000" class=""> input = </span>"Now is the time for all good men to come to the aid of the party"</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">// basic regex match</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: #4f8187" class="">input</span><span style="color: #000000" class="">[</span>"\\w+"<span style="color: #000000" class="">] != </span><span style="color: #ba2da2" class="">nil</span></div><div class=""><span style="color: #ba2da2" class=""><br class=""></span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">// replace by assignment</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><span style="color: #4f8187" class="">input</span><span style="color: #000000" class="">[</span>"men"<span style="color: #000000" class="">] = </span>"folk"</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class="">print<span style="color: #000000" class="">(</span><span style="color: #4f8187" class="">input</span><span style="color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">// indiviual groups can be accessed</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><span style="color: #4f8187" class="">input</span><span style="color: #000000" class="">[</span>"(all) (\\w+)"<span style="color: #000000" class="">, </span><span style="color: #272ad8" class="">2</span><span style="color: #000000" class="">]</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">// and assigned to</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><span style="color: #4f8187" class="">input</span><span style="color: #000000" class="">[</span>"the (\\w+)"<span style="color: #000000" class="">, </span><span style="color: #272ad8" class="">1</span><span style="color: #000000" class="">] = </span>"_$1_"</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class="">print<span style="color: #000000" class="">(</span><span style="color: #4f8187" class="">input</span><span style="color: #000000" class="">)</span></div></div><div class=""><span style="color: #000000" class=""><br class=""></span></div><div class=""><div class="">Operators only get you so far though. This is the point where I wished</div><div class="">it was possible to define a setter for a subscript type without a getter.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">// capitalising words using closure</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><span style="color: #3e1e81" class="">print</span><span style="color: #000000" class="">(</span><span style="color: #4f8187" class="">input</span><span style="color: #000000" class="">.</span><span style="color: #31595d" class="">replacing</span><span style="color: #000000" class="">(pattern: </span>"(_?)(\\w)(\\w*)"<span style="color: #000000" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> (groups, stop) <span style="color: #ba2da2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">return</span> groups[<span style="color: #272ad8" class="">1</span>]!+groups[<span style="color: #272ad8" class="">2</span>]!.<span style="color: #3e1e81" class="">uppercased</span>()+groups[<span style="color: #272ad8" class="">3</span>]!</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">})</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">// parsing a properties file</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> props = <span style="color: #d12f1b" class="">"""</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> name1 = value1</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> name2 = value2</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> """</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">var</span> params = [<span style="color: #703daa" class="">String</span>: <span style="color: #703daa" class="">String</span>]()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">for</span> groups <span style="color: #ba2da2" class="">in</span> <span style="color: #4f8187" class="">props</span>.<span style="color: #31595d" class="">matching</span>(pattern: <span style="color: #d12f1b" class="">"(\\w+)\\s*=\\s*(.*)"</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #4f8187" class="">params</span>[<span style="color: #703daa" class="">String</span>(groups[<span style="color: #272ad8" class="">1</span>]!)] = <span style="color: #703daa" class="">String</span>(groups[<span style="color: #272ad8" class="">2</span>]!)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div></div><div class=""><br class=""></div><div class="">I prepared a playground taking this idea to it’s illogical conclusion.</div><div class=""><br class=""></div><div class=""></div></body></html>