<?xml version="1.0" encoding="UTF-8"?><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>Die Welt ist gar nicht so. &#187; regex</title>
	<atom:link href="http://blog.dieweltistgarnichtso.net/tag/regex/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.dieweltistgarnichtso.net</link>
	<description>Sie ist ganz anders.</description>
	<lastBuildDate>Mon, 23 Sep 2013 15:41:20 +0000</lastBuildDate>
	<language>de-DE</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.35</generator>
	<item>
		<title>Weniger Programmieren ist mehr</title>
		<link>http://blog.dieweltistgarnichtso.net/weniger-programmieren-ist-mehr</link>
		<comments>http://blog.dieweltistgarnichtso.net/weniger-programmieren-ist-mehr#comments</comments>
		<pubDate>Tue, 08 Nov 2011 16:36:12 +0000</pubDate>
		<dc:creator><![CDATA[erlehmann]]></dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[simplestthingthatworks]]></category>

		<guid isPermaLink="false">http://blog.dieweltistgarnichtso.net/?p=4127</guid>
		<description><![CDATA[Wenn ein Problem mit Hilfe eines Computerprogramms bearbeitet werden soll, ist es oft hilfreich, darüber nachzudenken, welcher Ansatz so einfach ist, dass er gerade so funktioniert. Eingeweihte nennen dies The Simplest Thing that Could Possibly Work. Diesem Paradigma folgende Lösungen &#8230; <a href="http://blog.dieweltistgarnichtso.net/weniger-programmieren-ist-mehr">Weiterlesen <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>
Wenn ein Problem mit Hilfe eines Computerprogramms bearbeitet werden soll, ist es oft hilfreich, darüber nachzudenken, welcher Ansatz so einfach ist, dass er <em>gerade so</em> funktioniert. <span title="Altschuchteln">Eingeweihte</span> nennen dies <a href="http://en.wikiquote.org/wiki/Ward_Cunningham#The_Simplest_Thing_that_Could_Possibly_Work"><i lang="en">The Simplest Thing that Could Possibly Work</i></a>.
</p>
<p>
Diesem Paradigma folgende Lösungen können selbst dann gut funktionieren, wenn sie auf den ersten Blick absurd wirken: Die Frage <a href="http://www.peterkroener.de/spamschutz-leichtgemacht/"><i>Was ist der Vorname von Franz Beckenbauer?</i> schützt gegen <i lang="en">spam</i></a> nämlich genau so gut wie ein <a href="http://de.wikipedia.org/wiki/Akismet">komplexes Analyse-<i>plugin</i></a>. Auch die Methode der <a href="http://riesenmaschine.de"><i>Riesenmaschine</i></a>, neue Kommentare gegen eine kurze Liste unerwünschter Wortbestandteile (etwa <i>!!</i>, <i>lugschei</i>) zu prüfen, kommt ohne komplizierte Filtermodelle aus.
</p>
<p>
<a href="http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/">Wie es hingegen <em>nicht</em> geht, beschreibt Lea Verou</a> anhand der Aufgabe, das Doppelkreuz am Anfang von <abbr title="Uniform Resource Locator">URL</abbr>-Fragmenten zu entfernen. In einem Buch fand sie dazu den <i>JavaScript</i>-Schnipsel <code>location.hash.<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match">match</a>(/#(\w+)/)[1]</code>. Der enthält allerdings nicht nur einen unleserlichen <a href="http://de.wikipedia.org/wiki/Regul%C3%A4rer_Ausdruck">regulären Ausdruck</a>, sondern funktioniert auch nicht; so produziert die Verarbeitung des <a href="http://mathiasbynens.be/notes/html5-id-class">gültigen Werts</a> <i>#♥</i> einen Fehler. Verous in jedem Fall funktionierende Alternative lautet schlicht <code>location.hash.<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring">substring</a>(1)</code>.
</p>
<p>
Um Fehler dieser Art zu verstehen, sollte man wissen, dass <a href="http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html">viele Programmierer nicht programmieren können</a>. Damit ist nicht gemeint, dass sie unfähig sind, lauffähigen Code zu produzieren – sie haben jedoch Schwierigkeiten, ein <a href="http://www.codinghorror.com/blog/2006/07/separating-programming-sheep-from-non-programming-goats.html">zutreffendes mentales Modell der Programmlogik</a> zu entwickeln. Hinzu kommt <a href="http://haacked.com/archive/2007/02/27/Why_Cant_Programmers._Read.aspx">mangelnde Lesekompetenz</a>: So lehnen Programme etwa <a href="http://www.jacobsen.no/anders/blog/archives/2002/08/24/email_addresses_with_a_are_valid.html">Email-Adressen mit Plus-Zeichen</a> ab, weil die Entwickler <a href="http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx">nicht wissen, welche Zeichen erlaubt sind</a> – und dennoch überzeugt sind, das Richtige zu tun.
</p>
<p>
Dass jemand einen Algorithmus oder eine Spezifikation missverstanden hat, ist jedoch für Außenstehende nicht auf den ersten Blick erkennbar, <a href="http://de.wikipedia.org/wiki/Dunning-Kruger-Effekt">für einen selber sogar noch weniger</a>. Unabhängig vom Ego gilt aber: Programme nach dem <i lang="en">Simplest Thing</i>-Prinzip zu schreiben reduziert das Risiko, etwas Unverständliches zu produzieren – selbst für <span title="Die BESTEN der BESTEN der BESTEN.">jene, die sich für besonders clever halten</span>. Als Fingerübung habe ich mir daher vorgenommen, so weit wie möglich auf reguläre Ausdrücke zu verzichten.
</p>
<p>
<i lang="en">The Simplest Thing that Could Possibly Work</i> muss übrigens nicht unbedingt aus eigenem Code bestehen: Spezialisierte Programmbibliotheken zu nutzen spart nicht nur Zeit, sondern reduziert ebenfalls die Fehlerrate. Faustregel: Wer komplexe Formate wie <abbr title="Hypertext Markup Language">HTML</abbr> parsen oder generieren möchte, sollte besser <a href="http://code.google.com/p/html5lib/">existierende Werkzeuge</a> benutzen, als sich selbst Gedanken zu machen – sonst besteht das Risiko, eine <a href="http://regex.info/blog/2006-09-15/247">beherrschte Technik in einem Bereich anzuwenden, für den sie vollkommen unpassend ist</a> (<a href="https://github.com/matthiasr/logformat/pull/4">Patch für Negativ-Beispiel</a>).
</p>
<p>
Gleich doppelt beleuchtet diese Kategorie <a href="https://github.com/juuso/BozoCrack"><i>BozoCrack</i></a> – ein Programm, das mit <a href="http://de.wikipedia.org/wiki/Md5"><abbr title="Message-Digest Algorithm 5">MD5</abbr></a> unkenntlich gemachte Passphrasen nicht selbst knackt, sondern Google bemüht (danke, <i>plomlompom</i>): Es benutzt nicht nur eine externe Resource, sondern zeigt auch, dass die <abbr title="Message-Digest Algorithm 5">MD5</abbr> nutzenden Entwickler nicht verstanden haben, <a href="http://codahale.com/how-to-safely-store-a-password/">wie Passwörter gespeichert werden sollten</a>.
</p>]]></content:encoded>
			<wfw:commentRss>http://blog.dieweltistgarnichtso.net/weniger-programmieren-ist-mehr/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Linkschleuder (4)</title>
		<link>http://blog.dieweltistgarnichtso.net/linkschleuder-4</link>
		<comments>http://blog.dieweltistgarnichtso.net/linkschleuder-4#comments</comments>
		<pubDate>Tue, 17 Nov 2009 18:58:29 +0000</pubDate>
		<dc:creator><![CDATA[erlehmann]]></dc:creator>
				<category><![CDATA[Linkschleuder]]></category>
		<category><![CDATA[avr]]></category>
		<category><![CDATA[c-base]]></category>
		<category><![CDATA[canalplus]]></category>
		<category><![CDATA[har09]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[juden]]></category>
		<category><![CDATA[porn]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[sex]]></category>
		<category><![CDATA[theora]]></category>
		<category><![CDATA[youtube]]></category>

		<guid isPermaLink="false">http://blog.dieweltistgarnichtso.net/?p=1382</guid>
		<description><![CDATA[Großartige Canal Plus-Werbung Electric Razors and Orthodox Judaism What if having sex made you lose your memory ? (via) The truth about regex-based HTML parsers Auf der HAR 2009 druckte man mit einem Reprap Plastikgeld und Handschellenschlüssel In Defense of &#8230; <a href="http://blog.dieweltistgarnichtso.net/linkschleuder-4">Weiterlesen <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<ul>
	<li><a href="http://www.youtube.com/watch?v=X7MVtgXMclI">Großartige <i>Canal Plus</i>-Werbung</a></li>

	<li><a href="http://en.wikipedia.org/wiki/Shaving_in_judaism#Electric_razors_and_Orthodox_Judaism">Electric Razors and Orthodox Judaism</a></li>

	<li><a href="http://edition.cnn.com/2009/HEALTH/11/04/transient.global.amnesia/index.html">What if having sex made you lose your memory ?</a> (<a href="http://www.thefrisky.com/post/246-what-if-having-sex-made-you-lose-your-memory/">via</a>)</li>

	<li><a href="http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">The truth about regex-based <abbr title="Hypertext Markup Language">HTML</abbr> parsers</a></li>

	<li><a href="http://blog.erikdebruijn.nl/archives/114-With-great-3D-printing-power-comes-great-responsibility-Printing-keys-coins.html">Auf der <abbr title="Hacking at Random">HAR</abbr> 2009 druckte man mit einem Reprap Plastikgeld und Handschellenschlüssel</a></li>

	<li><a href="http://reason.com/archives/2009/10/27/in-defense-of-extreme-porn/singlepage">In Defense of Extreme Pornography</a> — Why Janet Romano and Rob Zicari have no business <a href="http://en.wikipedia.org/wiki/United_States_v._Extreme_Associates">being in federal prison</a>.
(<a href="http://twitter.com/maltewelding/status/5365994805">via</a>)</li>

	<li><a href="http://www.madore.org/~david/computers/quine.html">Wie man Quines programmiert</a></li>

	<li><a href="http://rossum.posterous.com/avr-homebrew-device-with-iphone-aspirations">AVR Homebrew Device with Iphone-Aspirations</a> (<a href="via http://hackaday.com/2009/11/03/8-bit-device-quenches-iphone-envy/">via</a>)</li>

	<li>Die von <i>trash</i> designte <a href="http://wiki.c-base.org/coredump/CeVa"><i>c-base</i>-Schriftart</a>
</li>

	<li><a href="http://www.0xdeadbeef.com/weblog/?p=1518">Bringing Theora to Youtube (the hard way)</a></li>

	<li>Eine Suchmaschine für <a href="http://de.wikipedia.org/wiki/Dia_(Software)">Dia</a>-Formen: <a href="http://diashapes.appspot.com/">Diashapes</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://blog.dieweltistgarnichtso.net/linkschleuder-4/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Constructing a regular expression that matches URIs</title>
		<link>http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris</link>
		<comments>http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris#comments</comments>
		<pubDate>Thu, 26 Jun 2008 19:27:25 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Originärer Inhalt]]></category>
		<category><![CDATA[Technik]]></category>
		<category><![CDATA[gajim]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[uri]]></category>

		<guid isPermaLink="false">http://blog.dieweltistgarnichtso.net/?p=34</guid>
		<description><![CDATA[URI matching is commonly needed, most notably for URL matching &#8211; chat clients use this to create links in what is otherwise plain (and not hyper-) text. However, many regexes that are supposed to do exactly that fail on encountering &#8230; <a href="http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris">Weiterlesen <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>
<a href="http://en.wikipedia.org/wiki/URI">URI</a> matching is commonly needed, most notably for <a href="http://en.wikipedia.org/wiki/URI">URL</a> matching &#8211; chat clients use this to create links in what is otherwise plain (and not hyper-) text. However, many regexes that are supposed to do exactly that fail on encountering uncommon, yet valid characters, because programmers don&#8217;t follow the RFC (many probably don&#8217;t even read it).
</p>
<p>
Additionally, users are <em>stupid</em>: While according to <a href="http://tools.ietf.org/html/rfc3986">RFC 3986</a>, <a href="http://en.wikipedia.org/wiki/Brackets#Angle_brackets_or_chevrons_.E2.8C.A9.C2.A0.E2.8C.AA"><em>chevrons</em></a> should be used to designate URIs, often people use <a href="http://en.wikipedia.org/wiki/Brackets#Parentheses__.28_.29"><em>parentheses</em></a>. When developers try to compensate for this, they create undesired &#8211; and more than often unexpected &#8211; behaviour: Links created from <em>perfectly valid URIs</em> are suddenly broken &#8211; see, for example, <a href="http://trac.gajim.org/ticket/3715">the chat client Gajim</a> (and also the bugtracker / wiki Trac).
</p><p>
According to RFC 3986, <a href="http://tools.ietf.org/html/rfc3986#section-1.1.1">subsection 1.1.1</a> , <q>URI[s] begin[s] with a <em>scheme name</em></q>, which, according to <a href="http://tools.ietf.org/html/rfc3986#section-3.1">subsection 3.1</a> <q>consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus (&#8220;+&#8221;), period (&#8220;.&#8221;), or hyphen (&#8220;-&#8220;)</q>. Therefore, the correct regular expression for a scheme name is
<code>
[A-Za-z][A-Za-z0-9\+\.\-]*
</code>
.
</p>
<p>
After the scheme name, a colon (&#8220;:&#8221;) follows &#8211; the rest is scheme-specifix synthax; according to sections <a href="http://tools.ietf.org/html/rfc3986#section-2.2">2.2</a> and <a href="http://tools.ietf.org/html/rfc3986#section-2.3">2.3</a> we only know it uses a limited set of characters, namely those reserved for delimiting data (&#8220;:&#8221;, &#8220;/&#8221;, &#8220;?&#8221;, &#8220;#&#8221;, &#8220;[&#8220;, &#8220;]&#8221;, &#8220;@&#8221;, &#8220;!&#8221;, &#8220;$&#8221;, &#8220;&amp;&#8221;, &#8220;&#8216;&#8221;, &#8220;(&#8220;, &#8220;)&#8221;, &#8220;*&#8221;, &#8220;+&#8221;, &#8220;,&#8221;, &#8220;;&#8221;, &#8220;=&#8221;) and unreserved ones, which <q>include uppercase and lowercase
   letters, decimal digits, hyphen, period, underscore, and tilde</q>. This extends the regular expression to
<code>
[A-Za-z][A-Za-z0-9\+\.\-]*:[A-Za-z0-9\.\-_~:/\?#\[\]@!\$&amp;'\(\)\*\+,;=]+
</code>
&#8211; metacharacters (&#8220;[&#8220;, &#8220;\&#8221;, &#8220;$&#8221;, &#8220;.&#8221; &#8220;?&#8221;, &#8220;*&#8221;, &#8220;+&#8221; &#8220;(&#8220;, &#8220;)&#8221;) <ins>and the range specifier (&#8220;-&#8220;)</ins> properly encoded, of course.
</p>
<p>
&#8220;But wait&#8221;, you may be thinking right now, &#8220;how can I include other characters &#8211; umlauts, for example &#8211; in URIs, then ?&#8221; Well, you <em>can&#8217;t</em>. But you <em>can</em> describe a resource that contains characters not listed in the above paragraph by means of <em>percent-encoding</em>, a method detailed in <a href="http://tools.ietf.org/html/rfc3986#section-2.1">section 2.1</a> to <q>represent a data octet in a component when that octet&#8217;s corresponding character is outside the allowed set or is being used as a delimiter of, or within, the component</q>. A percent-encoded character takes the form of a percent character (&#8220;%&#8221;), followed by two hexadecimal digits &#8211; the space character, for example, is encoded as &#8220;%20&#8243;. This gives us the expression
<code>
%[A-Fa-f0-9]{2}
</code>
, which can be added to the existing URI matching expression:
<code>
[A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-z0-9\.\-_~:/\?#\[\]@!\$&amp;'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+
</code>
will catch any valid URI (and probably some invalid ones too).
</p>
<p>
Now, what about the parenthesis problem that surfaced in the beginning ? A simple solution is just to define an additional expression that matches on URIs, but only if they are preceeded by an opening parenthesis (this feature is called &#8220;positive lookbehind&#8221;) and followed by a closing parenthesis (&#8220;positive lookahead&#8221;). We get
<code>
(?&lt;=\()[A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-z0-9\.\-_~:/\?#\[\]@!\$&amp;'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+(?=\))
</code>
.
Combining the two massive expressions by means of a simple <em>OR</em> yields the final result:
<code>
((?&lt;=\()[A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-z0-9\.\-_~:/\?#\[\]@!\$&amp;'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+(?=\)))|([A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-z0-9\.\-_~:/\?#\[\]@!\$&amp;'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+)
</code>
.
</p>

<ins><p>
<strong>Update:</strong> Shortly after Gajim <a href="http://trac.gajim.org/changeset/9845">implemented</a> it, it became clear that I had forgotten to escape the literal minus sign (&#8220;-&#8220;), which wouldn&#8217;t be matched then. This has since been corrected (in this post and <a href="http://trac.gajim.org/changeset/9852">in Gajim</a>).
</p></ins>
<ins datetime="2013-01-24T21:24:11+00:00">
<p>
Since <a href="http://www.regular-expressions.info/posixbrackets.html#eq">regular expressions can be locale-sensitive</a>, I suggest using the <a href="http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html#tag_07_02"><i>C</i> locale</a>.
</p>
</ins>]]></content:encoded>
			<wfw:commentRss>http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
