{"id":2269,"date":"2006-02-07T12:43:00","date_gmt":"2006-02-07T12:43:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/trivial-persuits\/"},"modified":"2016-07-28T10:48:45","modified_gmt":"2016-07-28T10:48:45","slug":"trivial-persuits","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/other\/trivial-persuits\/","title":{"rendered":"Trivial Persuits"},"content":{"rendered":"<p>Once again I find myself penning a missive on trivia, rather than matters of import. Still, now we&#8217;re here&#8230;<\/p>\n<p>  Regular expressions. To a large extent, I hate them. To begin with, they are a complete misnominer. They are neither regular, nor expressions. Don&#8217;t believe me? To demonstrate this I&#8217;ll consult an authority on such topics. The Oxford English Dictionary would be handy, but being incredibly lax I don&#8217;t seem to have a copy lying around. However, dictionary.com will do&#8230;<\/p>\n<p><b>reg&#194;&#183;u&#194;&#183;lar<\/b><br \/><i>adj.<\/i><\/p>\n<p> &#160;&#160; 1. Customary, usual, or normal: the train&#8217;s regular schedule.<br \/> &#160;&#160; 2. Orderly, even, or symmetrical: regular teeth.<br \/> &#160;&#160; 3. In conformity with a fixed procedure, principle, or discipline.<br \/> &#160;&#160; 4. Well-ordered; methodical: regular habits.<br \/> &#160;&#160; 5. Occurring at fixed intervals; periodic: regular payments.<br \/> &#160;&#160; 6.<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1. Occurring with normal or healthy frequency.<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 2. Having bowel movements or menstrual periods with normal or healthy frequency.<br \/> &#160;&#160; 7. Not varying; constant.<br \/> &#160;&#160; 8. Formally correct; proper.<br \/> &#160;&#160; 9. Having the required qualifications for an occupation: not a regular lawyer.<br \/> &#160; 10. Informal. Complete; thorough: a regular scoundrel.<br \/> &#160; 11. Informal. Good; nice: a regular guy.<br \/> &#160; 12. Botany. Having symmetrically arranged parts of similar size and shape: regular flowers.<br \/> &#160; 13. Grammar. Conforming to the usual pattern of inflection, derivation, or word formation.<br \/> &#160; 14. Ecclesiastical. Belonging to a religious order and bound by its rules: the regular clergy.<br \/> &#160; 15. Mathematics.<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1. Having equal sides and equal angles. Used of polygons.<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 2. Having faces that are congruent regular polygons and congruent polyhedral angles. Used of polyhedrons.<br \/> &#160; 16. Belonging to or constituting the permanent army of a nation.<\/p>\n<p><b>ex&#194;&#183;pres&#194;&#183;sion<\/b><br \/><i>n.<\/i><\/p>\n<p> &#160;&#160; 1. The act of expressing, conveying, or representing in words, art, music, or movement; a manifestation: an expression of rural values.<br \/> &#160;&#160; 2. Something that expresses or communicates: Let this plaque serve as an expression of our esteem.<br \/> &#160;&#160; 3. Mathematics. A symbol or combination of symbols that represents a quantity or a relationship between quantities.<br \/> &#160;&#160; 4. The manner in which one expresses oneself, especially in speaking, depicting, or performing.<br \/> &#160;&#160; 5. A particular word or phrase: &#8220;an old Yankee expression&#8230; &#8216;Stand up and be counted'&#8221; (Charles Kuralt).<br \/> &#160;&#160; 6. The outward manifestation of a mood or a disposition: My tears are an expression of my grief.<br \/> &#160;&#160; 7. A facial aspect or a look that conveys a special feeling: an expression of scorn.<br \/> &#160;&#160; 8. The act of pressing or squeezing out.<br \/> &#160;&#160; 9. Genetics. The act or process of expressing a gene.<br \/>  Now, I know someone&#8217;s going to tell me that if you look up &#8220;regular expression&#8221; on dictionary.com it gives you the &#8220;proper&#8221; definition. But to that I say, bah. There&#8217;s far too much rather pants terminology in the world, particularly in the computing arena. Next you&#8217;ll be telling me that &#8220;non-deterministic finite state automaton&#8221; is easier to say than &#8220;state machine&#8221;. And put those 5-tuples away, you don&#8217;t know where they&#8217;ve been. It&#8217;s not big and it&#8217;s not clever.<\/p>\n<p>  Without getting overly semantic (though I may deviate from the canonical definition of &#8220;overly&#8221; here) regular expressions are not regular. There are at least three different flavours of which I&#8217;m aware, all of which have their own magical incantations: Perl-compatible (as seen in Perl &#8211; shocking I know &#8211; PHP, and the open source PCRE engine, http:\/\/pcre.org); Microsoft (as seen in Visual Studio and .NET&#8217;s regular expression library); and Java (as seen in, well, Java). &#8220;Expressions&#8221; expressed in one dialect are not necessarily freely transferable to the other. This doesn&#8217;t meet any sensible definition of regular that I&#8217;m aware of. &#8220;Self consistent&#8221; would perhaps be more appropriate.<\/p>\n<p>  Nor are they expressions. Expressions tend, by definition, to express something to somebody. I blame Perl entirely for this. Any &#8220;high level&#8221; language in which software can be expressed entirely without reference to any alphabet used by a civilisation, past or present, cannot claim to adequately express anything. If I wanted unreadable gumf, I&#8217;d write software in pure machine code. With a decent lookup table and a few cups of coffee it wouldn&#8217;t be much less straightforward.<\/p>\n<p> However, despite their inadequate definition and manifest unreadability, they do have some extraordinary merits.<\/p>\n<p> Today, for example, 10 minutes of faffing around trying to remember which bits of the Visual Studio RE (see, even handy, descriptive phrases like &#8220;regular expression&#8221; are too long to type occasionally) syntax are Microsoft-specific, and which are generally applicable, in which time I eventually resorted to the help&#8230;proved to be exceedingly handy.<\/p>\n<p> Our software tends to be sold in many international markets, and thus internationalisation (I18N; which isn&#8217;t a regular expression, but another bludgeoningly stupid acronym bordering on haxx0r lingo) and localisation (L10N, if I recall&#8230;but please don&#8217;t tell me if I&#8217;m wrong, I&#8217;d like to keep it as a surprise) is an issue for us. All the strings in our source code need to be pulled out of resources, rather than hard-coded. This is sensible practice anyway, but means you end up not being able to express text literally but dereferencing it.<\/p>\n<p> Previously I was doing as follows. Rather than expressing, eg.<\/p>\n<p>namespace MyCompany.MyApp<br \/> {<br \/> &#160;&#160;&#160; class HelloWorld()<br \/>&#160;&#160;&#160; {<br \/>&#160;&#160;&#160; &#160;&#160;&#160; HelloWorld()<br \/>&#160;&#160;&#160; &#160;&#160;&#160; {<br \/>&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160; string name = &#8220;world&#8221;;<br \/>&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160; string message = string.Format(&#8220;Hello, {0}&#8221;,name);<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Debug.WriteLine(message);<br \/>&#160;&#160;&#160; &#160;&#160;&#160; }<br \/>&#160;&#160;&#160; }<br \/> }<\/p>\n<p>One might write:<\/p>\n<p>namespace MyCompany.MyApp<br \/> {<br \/> &#160;&#160;&#160; class HelloWorld<br \/> &#160;&#160;&#160; {<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160; private static ResourceManager m_Resources = new ResourceManager(&#8220;MyCompany.MyApp.HelloWorldClass_Resources&#8221;);<\/p>\n<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160; HelloWorld()<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160; {&#160;&#160;&#160;&#160;&#160;  <br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; string name = m_Resources.GetString(&#8220;Name.Text&#8221;);<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; string message = string.Format( m_Resources.GetString(&#8220;HelloWorldMessage.Text&#8221;), name );<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Debug.WriteLine(message);<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br \/> &#160;&#160;&#160;&#160; }<br \/> }<\/p>\n<p> Which is not overlong, but is a bit less expressive. Note how I decided at some point to adopt a &#8220;dotted text&#8221; convention for my resource string names at some point. Not a particularly clever idea, in retrospect, since most of them end in &#8220;.Text&#8221;, except when I decide they should end in &#8220;.Caption&#8221; as they represent eg. a button caption on a form. Of course then you discover that the button&#8217;s property is also called Text rather than Caption, but there&#8217;d be retyping involved in changing it&#8230;and so on.<\/p>\n<p> Nowadays I&#8217;ve adopted a rather simpler syntax. Assuming the existence of a handy UI class with some helpful static methods, I tend to write as follows:<\/p>\n<p>namespace MyCompany.MyApp<br \/> {<br \/> &#160;&#160;&#160; class HelloWorld<br \/> &#160;&#160;&#160; {<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160; private static ResourceStrings S = UI.GetResourceStrings(typeof(HelloWorld));<\/p>\n<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160; HelloWorld()<br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160; {&#160;&#160;&#160;&#160;&#160;  <br \/> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Debug.WriteLine( string.Format( S[&#8220;HelloX&#8221;], S[&#8220;World&#8221;] ) );<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br \/> &#160;&#160;&#160;&#160; }<br \/> }<\/p>\n<p>  Which I think is remarkably tidier. Probably because it reminds me of the Visual C++ _T() macro to encapsulate text which, at compile time, may or may not turn out to be Unicode.<\/p>\n<p> Anyhoo. The point was, that having adopted this new convention I was faced with having to change a number of source files to use the new style rather than the old.<\/p>\n<p> Conventional search-and-replace is of little value here. Replacing m_Resources.GetString(&#8220;SomeText&#8221;) with S[&#8220;SomeText&#8221;] can&#8217;t quite be done. You could try it in two parts, replacing m_Resources.GetString(&#8221; with S[&#8220;, and replacing &#8220;) with &#8220;], but you&#8217;re bound to find a number of &#8220;)&#8217;s littered all over the shop which you&#8217;ll have to fix manually, that belong to other things than a GetString() call.<\/p>\n<p> With regular expressions, however, you can make the hougan work his vodun for you. Since Visual Studio&#8217;s Find\/Replace widget supports regular expressions, one can incant the following:<\/p>\n<p> Find all&#160;&#160; m_Resources.GetString({[^)]#})<br \/> And replace with&#160;&#160; S[(0,1)]<\/p>\n<p> Once the sparks have settled down and the broom has finished sweeping the magician&#8217;s workshop, one finds cheerfully that tedium has been averted and it&#8217;s time for lunch.<\/p>\n<p> For those who are relentlessly interested in regular expressions, I&#8217;ve also just found the aptly named http:\/\/www.regular-expressions.info\/, which does exactly what it says on the tin, if URL&#8217;s were to come in tins. URL. Don&#8217;t get me started on that little moniker&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once again I find myself penning a missive on trivia, rather than matters of import. Still, now we&#8217;re here&#8230; Regular expressions. To a large extent, I hate them. To begin with, they are a complete misnominer. They are neither regular, nor expressions. Don&#8217;t believe me? To demonstrate this I&#8217;ll consult an authority on such topics&#8230;.&hellip;<\/p>\n","protected":false},"author":46780,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2269","post","type-post","status-publish","format-standard","hentry","category-other"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46780"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2269"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2269\/revisions"}],"predecessor-version":[{"id":41359,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2269\/revisions\/41359"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2269"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}