<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.xsl" type="text/xsl"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>regex &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Fri, 06 Feb 2026 22:16:40 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>regex &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[Regular Expressions make me feel like a powerful wizard - and that's not a good thing]]></title>
		<link>https://shkspr.mobi/blog/2023/02/regular-expressions-make-me-feel-like-a-powerful-wizard-and-thats-not-a-good-thing/</link>
					<comments>https://shkspr.mobi/blog/2023/02/regular-expressions-make-me-feel-like-a-powerful-wizard-and-thats-not-a-good-thing/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Mon, 06 Feb 2023 12:34:19 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[regex]]></category>
		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=44537</guid>

					<description><![CDATA[(This is a rant because I&#039;m exhausted after debugging something. If you&#039;ve made RegEx your whole personality, I&#039;m sorry.)  The other day I had to fix a multi-line Regular Expression (RegEx). After a few hours of peering at it with a variety of tools, I finally understood the problem. Getting that deep into the esoteric mysteries made me feel like a powerful wizard with complete mastery of my…]]></description>
										<content:encoded><![CDATA[<p>(This is a rant because I'm exhausted after debugging something. If you've made RegEx your whole personality, I'm sorry.)</p>

<p>The other day I had to fix a multi-line Regular Expression (RegEx). After a few hours of peering at it with a variety of tools, I finally understood the problem. Getting that deep into the esoteric mysteries made me feel like a powerful wizard with complete mastery of my domain. And I think that's dangerous.</p>

<p>I'm sure we've all read a story about a witch or wizard who distractedly substitutes eye-of-newt with iron-ute with disastrous consequences.  Humans are easily confused.  And confusion leads to unexpected mistakes.</p>

<p>Look, most humans are very bad at reading compiled code. Without any external tools - can you tell me what the following code does?</p>

<pre><code class="language-bin">0000000 c031 d88e c08e 15be b47c ac0e 003c 0474
0000010 10cd f7eb 48f4 6c65 6f6c 202c 6f57 6c72
0000020 2164 0a0d 0000 0000 0000 0000 0000 0000
0000030 0000 0000 0000 0000 0000 0000 0000 0000
*
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55
0000200
</code></pre>

<p>No. Of course not<sup id="fnref:not"><a href="https://shkspr.mobi/blog/2023/02/regular-expressions-make-me-feel-like-a-powerful-wizard-and-thats-not-a-good-thing/#fn:not" class="footnote-ref" title="You can read the original code which is MIT Licenced." role="doc-noteref">0</a></sup>. That's why we write code in a more human readable language and then compile it to computer readable instructions.</p>

<p>Regular Expressions are a sort-of halfway house. They're <em>slightly</em> readable by humans - but written in such a terse vocabulary as to be mostly unintelligible without concentration. There's no space for comments. Different engines have variable support for all their functions. They are a symbolic language with unhelpfully indecipherable and inconsistent symbols.</p>

<p>As a result, once the RegEx becomes more than trivially complex they're hard for most humans to understand. That makes them difficult to debug. It also makes it difficult to add or remove functionality.</p>

<p>I genuinely - and possibly misguidedly - believe that even something like <code>^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$</code> might just as well be written in <a href="https://esolangs.org/wiki/Brainfuck">BrainFuck</a>.</p>

<p>My contention is that <em>almost all</em> RegExs would be better served by more human readable code and that the very existence of <a href="https://regex101.com/">RegEx101.com</a> ought to bring shame on our industry.</p>

<p>Here are some positive use-cases for RegEx:</p>

<ul>
<li>You want to show off how smart you are.</li>
<li>You need maximum efficiency when combing through a billion lines of text.</li>
<li>You have a desire to build something hard to debug.</li>
<li>You don't have lots of printer paper and need to make your code as terse as possible.</li>
<li>You think if/else and switch/case statements are the mark of a diseased mind.</li>
<li>You don't trust compilers.</li>
</ul>

<p>I know what you're thinking: "This guy's too stupid to get regular expressions!"  Yes. Yes I am. So are most people.</p>

<p>What I'm getting at is that source code is designed to be read and edited by busy and distracted humans.  We should be writing intelligible code for each other and letting computers do the boring work of making it more efficient.</p>

<p>You don't have to agree with me. That's fine. But, perhaps you'll take note of the famous maxim from <a href="https://archive.org/details/newhackersdictio00raym/page/385/mode/1up?q=%22wizard+book%22">the "Wizard" book</a>:</p>

<blockquote><p>a computer language is not just a way of getting a computer to perform operations but rather that it is a novel formal medium for expressing ideas about methodology. Thus, programs must be written for people to read, and only incidentally for machines to execute.</p>

<p><a href="https://web.mit.edu/6.001/6.037/sicp.pdf">Structure and Interpretation of Computer Programs</a></p></blockquote>

<p>We are not wizards. Nor should we strive to be. The alchemists fell.</p>

<div id="footnotes" role="doc-endnotes">
<hr aria-label="Footnotes">
<ol start="0">

<li id="fn:not">
<p>You can <a href="https://github.com/AyrA/x86-hello-world">read the original code which is MIT Licenced</a>.&nbsp;<a href="https://shkspr.mobi/blog/2023/02/regular-expressions-make-me-feel-like-a-powerful-wizard-and-thats-not-a-good-thing/#fnref:not" class="footnote-backref" role="doc-backlink">↩︎</a></p>
</li>

</ol>
</div>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=44537&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2023/02/regular-expressions-make-me-feel-like-a-powerful-wizard-and-thats-not-a-good-thing/feed/</wfw:commentRss>
			<slash:comments>28</slash:comments>
		
		
			</item>
		<item>
		<title><![CDATA[When is a URL not a URL?]]></title>
		<link>https://shkspr.mobi/blog/2011/07/when-is-a-url-not-a-url/</link>
					<comments>https://shkspr.mobi/blog/2011/07/when-is-a-url-not-a-url/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Wed, 27 Jul 2011 11:37:57 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[urls]]></category>
		<guid isPermaLink="false">http://shkspr.mobi/blog/?p=4271</guid>

					<description><![CDATA[Summary  Twitter&#039;s way of linking URLs is broken.  It&#039;s annoying to users, and a pain in the arse to developers.  This quick post talks about the problem and offers a solution.  I&#039;ve raised a bug with Twitter and I hope you&#039;ll star it as important to you.   Preamble  A common trope in programming classes is &#34;how do you detect valid email address?&#34;  It should be obvious, right?  A string of text,…]]></description>
										<content:encoded><![CDATA[<h2 id="summary"><a href="https://shkspr.mobi/blog/2011/07/when-is-a-url-not-a-url/#summary">Summary</a></h2>

<p>Twitter's way of linking URLs is broken.  It's annoying to users, and a pain in the arse to developers.  This quick post talks about the problem and offers a solution.</p>

<p><a href="http://code.google.com/p/twitter-api/issues/detail?id=2240">I've raised a bug with Twitter</a> and I hope you'll star it as important to you.
<span id="more-4271"></span></p>

<h2 id="preamble"><a href="https://shkspr.mobi/blog/2011/07/when-is-a-url-not-a-url/#preamble">Preamble</a></h2>

<p>A common trope in programming classes is "<a href="http://www.regular-expressions.info/email.html">how do you detect valid email address</a>?"</p>

<p>It should be obvious, right?  A string of text, an @, a domain - probably ending in .com.
As it turns out, it's not that simple.  "who+o'toole@invalid.museum" is a potentially valid address, for example.
There are literally thousands of ways to detect the potentially infinite variety of email addresses.</p>

<p>The same is true for URLs - and slavish adherence to guidelines is killing Twitter's usefulness.</p>

<h2 id="the-url-matching-problem"><a href="https://shkspr.mobi/blog/2011/07/when-is-a-url-not-a-url/#the-url-matching-problem">The URL Matching Problem</a></h2>

<p>Which of these strings should be turned into hyperlinks?</p>

<pre>www.bbc.co.uk

example.com

http://test

https://test.test

ftp://news.com
</pre>

<p>As it happens, Twitter only matches "https://test.test" and none of the others.</p>

<p><a href="https://twitter.com/edent/status/96172785436590080"><img src="https://shkspr.mobi/blog/wp-content/uploads/2011/07/URL-test-1.jpg" alt="" title="URL test 1" width="514" height="216" class="aligncenter size-full wp-image-4274"></a></p>

<p>Twitter's matching regex is, as far as I can tell, this</p>

<pre>If it starts with http:// or https:// and has a dot in it - it's a URL</pre>

<p>I think this is a serious weakness.  Twitter users are sharing URLs which their followers can't click on - Twitter is also linking to URLs which don't exist.</p>

<p>I've picked these examples more or less at random.
<a href="https://twitter.com/ianvisits/status/82712842112991232"><img src="https://shkspr.mobi/blog/wp-content/uploads/2011/07/URL-test-2.jpg" alt="" title="URL test 2" width="514" height="216" class="aligncenter size-full wp-image-4275"></a></p>

<p><a href="https://twitter.com/PeakChief/status/82722453767462912"><img src="https://shkspr.mobi/blog/wp-content/uploads/2011/07/URL-test-3.jpg" alt="" title="URL test 3" width="514" height="216" class="aligncenter size-full wp-image-4276"></a></p>

<h2 id="solution"><a href="https://shkspr.mobi/blog/2011/07/when-is-a-url-not-a-url/#solution">Solution?</a></h2>

<p>Much like the email regexes, I would take a much more lax approach.  Essentially, if it looks vaguely like a URL - link to it.</p>

<p>I would suggest the following rules:</p>

<ul>
    <li>If it starts with a protocol - http:// ftp:// tel: etc - create a hyperlink.</li>
    <li>If it starts with www. - create a hyperlink.</li>
    <li>If it ends . then a <a href="http://data.iana.org/TLD/tlds-alpha-by-domain.txt">valid TLD</a> - create a hyperlink.</li>
    <li>If it contains a <a href="http://data.iana.org/TLD/tlds-alpha-by-domain.txt">valid TLD</a> followed by a slash then some other characters - create a hyperlink.</li>
</ul>

<p>The "correct" method would then be for Twitter to perform an <a href="http://en.wikipedia.org/wiki/HTTP#Request_methods">HTTP HEAD request</a> to see if the URL is potentially valid.  There are three drawbacks to this.</p>

<ol>
    <li>It may place excessive load on Twitter's servers to process and cache these requests.</li>
    <li>The URL may be that of an Intranet site - and thus inaccessible to Twitter.</li>
    <li>The URL may be valid but temporarily inaccessible.</li>
</ol>

<p>Regardless of the method, surely it's inexcusable that "www.example.com" isn't detected as a URL whereas "http://bork.bork.bork" is?</p>

<h2 id="action"><a href="https://shkspr.mobi/blog/2011/07/when-is-a-url-not-a-url/#action">ACTION!</a></h2>

<p>If you think Twitter's approach to hyperlinks is wrong - please <a href="http://code.google.com/p/twitter-api/issues/detail?id=2240">make your voice heard at the bug report</a>.</p>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=4271&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2011/07/when-is-a-url-not-a-url/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title><![CDATA[Bugs in Twitter Text Libraries]]></title>
		<link>https://shkspr.mobi/blog/2010/03/bugs-in-twitter-text-libraries/</link>
					<comments>https://shkspr.mobi/blog/2010/03/bugs-in-twitter-text-libraries/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Wed, 31 Mar 2010 10:27:50 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[dabr]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[urls]]></category>
		<guid isPermaLink="false">http://shkspr.mobi/blog/?p=1924</guid>

					<description><![CDATA[The Twitter Engineering Team have a set of text processing classes which are meant to simplify and standardise the recognition of URLs, screen names, and hashtags.  Dabr makes use of them to keep in conformance with Twitter&#039;s style.  One of the advantages of the text processing is that it will recognise that www.example.com is a URL and automatically create a hyperlink. Considering that dropping…]]></description>
										<content:encoded><![CDATA[<p>The <a href="https://blog.twitter.com/engineering/en_us/a/2010/introducing-the-open-source-twitter-text-libraries">Twitter Engineering Team have a set of text processing classes</a> which are meant to simplify and standardise the recognition of URLs, screen names, and hashtags.  Dabr makes use of them to keep in conformance with Twitter's style.</p>

<p>One of the advantages of the text processing is that it will recognise that www.example.com is a URL and automatically create a hyperlink. Considering that dropping the "http://" represents 5% saving on Twitter's 140 character limit for messages, this is great.</p>

<p>So, I was mightily surprised to get <a href="http://twitter.com/schmmuck/status/11352406573">this bug report</a> from user "schmmuck"</p>

<p></p><div id="attachment_1927" style="width: 490px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-1927" class="size-full wp-image-1927" title="Dabr rendering error" src="https://shkspr.mobi/blog/wp-content/uploads/2010/03/Capture8_19_22.jpg" alt="Dabr rendering error" width="480" height="320"><p id="caption-attachment-1927" class="wp-caption-text">Dabr rendering error</p></div><p></p>

<p>How very odd...  This is how it looks on <a href="http://m.twitter.com/">m.twitter.com</a>.</p>

<p></p><div id="attachment_1926" style="width: 490px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-1926" class="size-full wp-image-1926" title="m.twitter rendering error" src="https://shkspr.mobi/blog/wp-content/uploads/2010/03/Capture8_20_48.jpg" alt="m.twitter rendering error" width="480" height="320"><p id="caption-attachment-1926" class="wp-caption-text">m.twitter rendering error</p></div><p></p>

<p>Twitter also use <a href="http://mobile.twitter.com/">mobile.twitter.com</a> for smartphones.  Here's how that site renders the text.</p>

<p></p><div id="attachment_1925" style="width: 490px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-1925" class="size-full wp-image-1925" title="mobile.twitter rendering error" src="https://shkspr.mobi/blog/wp-content/uploads/2010/03/Capture8_21_54.jpg" alt="mobile.twitter rendering error" width="480" height="320"><p id="caption-attachment-1925" class="wp-caption-text">mobile.twitter rendering error</p></div><p></p>

<p>Finally, let's take a look at the "canonical" rendering at Twitter.com</p>

<p></p><div id="attachment_1928" style="width: 410px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-1928" class="size-full wp-image-1928" title="Twitter rendering error" src="https://shkspr.mobi/blog/wp-content/uploads/2010/03/Twitter-rendering-error.jpg" alt="Twitter rendering error" width="400" height="213"><p id="caption-attachment-1928" class="wp-caption-text">Twitter rendering error</p></div><p></p>

<h2 id="the-problems"><a href="https://shkspr.mobi/blog/2010/03/bugs-in-twitter-text-libraries/#the-problems">The Problem(s)</a></h2>

<p>The first issue is inconsistency.&nbsp; Twitter ought to be using the same regex for each of its sites.&nbsp; It doesn't.&nbsp; This means that different developers will get divergent experiences.&nbsp; This leads to confusion, which leads to fear, which, as we all know, leads to anger.... and so forth.</p>

<p>Secondly, and more importantly, parsing is <em>hard</em>.&nbsp; There are so many edge cases that errors inevitably creep in.&nbsp; My post about hashtags explains the problems in defining what <em>should</em> be recognised.</p>

<p>So, based on what we've seen, should Twitter recognise any of the following as URLs?</p>

<p>news.bbc.co.uk - no www there.</p>

<p>invalid.name - a silly URL, but a valid one.</p>

<p>खोज.com - International domains contain more than just ASCII</p>

<p>All the above are valid - yet they're not recognised by Twitter.</p>

<h2 id="a-simple-solution"><a href="https://shkspr.mobi/blog/2010/03/bugs-in-twitter-text-libraries/#a-simple-solution">A (Simple) Solution?</a></h2>

<p>There is a <a href="http://www.iana.org/domains/root/db/">canonical list of TLDs</a> which is also available as a <a href="http://data.iana.org/TLD/tlds-alpha-by-domain.txt">plain text list</a>.</p>

<p>Any string containing a "." followed by a valid TLD, then followed by a space or "/" should be treated as a URL.</p>

<p>Your thoughts?</p>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=1924&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2010/03/bugs-in-twitter-text-libraries/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
