<?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>mgBlog &#187; Regex</title>
	<atom:link href="http://mgsimon.de/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://mgsimon.de</link>
	<description>Esc(ape), einfach ein anderes Blog über technische Spielerei ...</description>
	<lastBuildDate>Fri, 16 Jul 2010 04:55:04 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>AdWz WP-Plugin 18 &#8211; Regex &amp; PHP</title>
		<link>http://mgsimon.de/2009/03/16/adwz-wp-plugin-18-regex-php/</link>
		<comments>http://mgsimon.de/2009/03/16/adwz-wp-plugin-18-regex-php/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 07:25:06 +0000</pubDate>
		<dc:creator>mg</dc:creator>
				<category><![CDATA[AdWz]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://mgsimon.de/?p=801</guid>
		<description><![CDATA[(AdWz; Auf dem Weg zum) WP-Plugin 18 &#8211; Regex &#038; PHP Im diesen Teil der AdWz-Serie werden die regul&#228;ren Ausdr&#252;cke aus AdWz WP-Plugin 17 &#8211; Regex im Plugin mg404rewrite eingesetzt. mg404_PostProxy 1 2 3 4 5 function mg404_PostProxy&#40;$content&#41; &#123; $formregex = &#34;/&#34; . get_option&#40;'mg404_postproxy_formregex'&#41; . &#34;/i&#34;; $content = preg_replace_callback&#40;$formregex,'mg404_FormProcessor',$content&#41;; return $content; &#125; In der f&#252;r [...]]]></description>
			<content:encoded><![CDATA[<p><strong>(AdWz; Auf dem Weg zum) WP-Plugin 18 &#8211; Regex &#038; PHP</strong></p>
<p>Im diesen Teil der AdWz-Serie werden die regul&#228;ren Ausdr&#252;cke aus <a href="http://mgsimon.de/2009/03/12/adwz-wp-plugin-17-regex/">AdWz WP-Plugin 17 &#8211; Regex</a> im Plugin mg404rewrite eingesetzt.</p>
<p><strong>mg404_PostProxy</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mg404_PostProxy<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$formregex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mg404_postproxy_formregex'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/i&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$formregex</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'mg404_FormProcessor'</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-801"></span></p>
<p>In der f&#252;r den Ausgabepuffer angemeldeten Funktion (Zeile 1) wird der regul&#228;re Ausdruck (Zeile 2) aus der Konfiguration gelesen und f&#252;r die Suche aufbereitet. In PHP werden regul&#228;re Ausdr&#252;cke am Anfang und Ende durch ein <strong>/</strong> gekennzeichnet. Das Zeichen <strong>i</strong> f&#252;hrt diesen Ausdruck unabh&#228;ngig der Gro&#223;- / Kleinschreibung aus.</p>
<p>Die Funktion preg_replace_callback (Zeile 3) meldet f&#252;r diesen regul&#228;ren Ausdruck eine Funktion an. Sollte im Ausgabepuffer eine Form mit der Methode Post vorhanden sein, wird die angemeldete Funktion (mg404_FormProcessor) zur Auswertung ausgef&#252;hrt.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">mixed <span style="color: #990000;">preg_replace_callback</span>  <span style="color: #009900;">&#40;</span> mixed <span style="color: #000088;">$pattern</span>  <span style="color: #339933;">,</span> callback <span style="color: #000088;">$callback</span>  <span style="color: #339933;">,</span> mixed <span style="color: #000088;">$subject</span>  <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$limit</span>  <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> int <span style="color: #339933;">&amp;</span><span style="color: #000088;">$count</span>  <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p>Jeder Treffer wird der Funktion &#252;bergeben und ausgewertet zur&#252;ck gegeben. Am Ende wird der modifizierte Ausgabepuffer &#252;berschrieben und gesendet.</p>
<p><strong>mg404_FormProcessor</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mg404_FormProcessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$formtag</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$urlregex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mg404_postproxy_urlregex'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/i&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$urlregex</span><span style="color: #339933;">,</span><span style="color: #000088;">$formtag</span><span style="color: #339933;">,</span><span style="color: #000088;">$actionurl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$linkregex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mg404_postproxy_linkregex'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/i&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$linkregex</span><span style="color: #339933;">,</span><span style="color: #000088;">$actionurl</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$formtag</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$postproxyurl</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'siteurl'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mg404_postproxy_url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$formtag</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$actionurl</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$postproxyurl</span><span style="color: #339933;">,</span><span style="color: #000088;">$formtag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$formtag</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;input type='hidden' name='mg404_posturl' value='&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$actionurl</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'/&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$formtag</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;input type='hidden' name='mg404_level' value='&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span>get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mg404_postproxy_url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'/&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$formtag</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In der f&#252;r den regul&#228;ren Ausdruck angemeldeten Funktion (Zeile 1) wird der Treffer als Array &#252;bergeben. Mehrere Treffer w&#252;rden im Array durch Gruppierung (Klammerung) im regul&#228;ren Ausdruck &#252;bergeben, da in diesem Fall nur ein Treffer vorhanden sein kann wird der erste Index (0) in der Variable formtag (Zeile 2) abgelegt. Die URL der Aktion wird extrahiert (Zeile 4) und in actionurl zur weiteren Verarbeitung abgelegt. actionurl wird &#252;berpr&#252;ft (Zeile 6) und bei einem nicht vorhandenen Permalink wird die Funktion vorzeitig ohne Ver&#228;nderung verlassen (Zeile 7). Im anderen Fall wird das Form-Tag &#252;berarbeitet und zus&#228;tzlich der urspr&#252;nglichen URL der Aktion zur&#252;ck gegeben (Zeile 9-12).</p>
<p><strong>weiter &#8230;</strong></p>
<p>Der Ausgabepuffer wird der Konfiguration entsprechend ausgewertet. Zu guter Letzt muss PostProxy zur Simulation die angefragte URI &#252;berschreiben und die Datei index.php von WordPress einbetten. Ein weiteres Skript wird demn&#228;chst im n&#228;chsten Teil der AdWz-Serie erstellt.</p>
]]></content:encoded>
			<wfw:commentRss>http://mgsimon.de/2009/03/16/adwz-wp-plugin-18-regex-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AdWz WP-Plugin 17 &#8211; Regex</title>
		<link>http://mgsimon.de/2009/03/12/adwz-wp-plugin-17-regex/</link>
		<comments>http://mgsimon.de/2009/03/12/adwz-wp-plugin-17-regex/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 09:38:34 +0000</pubDate>
		<dc:creator>mg</dc:creator>
				<category><![CDATA[AdWz]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://mgsimon.de/?p=731</guid>
		<description><![CDATA[(AdWz; Auf dem Weg zum) WP-Plugin 17 &#8211; Regex Regex; regular expressions (regul&#228;re Ausdr&#252;cke) &#8211; in den 1960ern von Ken Thompson f&#252;r die Entwicklung von qed und grep zuerst benutzt &#8211; sind Suchmuster, um eine definierte Menge aus einer Zeichenkette zu extrahieren. Vom Suchen, Ersetzen bis Pr&#252;fen k&#246;nnen in vielen Editoren, mit Tools wie z.B. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>(AdWz; Auf dem Weg zum) WP-Plugin 17 &#8211; Regex</strong></p>
<p>Regex; regular expressions (regul&#228;re Ausdr&#252;cke) &#8211; in den 1960ern von Ken Thompson f&#252;r die Entwicklung von qed und grep zuerst benutzt &#8211; sind Suchmuster, um eine definierte Menge aus einer Zeichenkette zu extrahieren. Vom Suchen, Ersetzen bis Pr&#252;fen k&#246;nnen in vielen Editoren, mit Tools wie z.B. grep und in so gut wie allen Programmiersprachen regul&#228;re Ausdr&#252;cke verwendet werden. </p>
<p>Beispiel eines regul&#228;ren Ausdrucks zur &#220;berpr&#252;fung einer E-mail-Adresse in richtiger Schreibweise (Syntax).</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">^([a-zA-Z0-9]((\.|\-)?[a-zA-Z0-9])*)@([a-zA-Z]((\.|\-)?[a-zA-Z0-9])*)\.([a-zA-Z]{2,8})$</pre></div></div>

<p>Auf den ersten Blick kompliziert, aber angefreundet mit der Syntax kann schnell mit diesem m&#228;chtigen Tool viel Arbeit gespart werden.</p>
<p>Ohne weiter auf das o.a. Beispiel einzugehen werden f&#252;r die Erweiterung des Plugins zur Verarbeitung der Post-Forms an Permalinks einige regul&#228;re Ausdr&#252;cke verwendet.</p>
<p><span id="more-731"></span></p>
<p><strong>mg404rewrite</strong></p>
<p>Im ersten Schritt werden alle Formulare mit der Methode Post gesucht. Die URL der Aktion wird extrahiert und zur weiteren Verarbeitung &#252;berpr&#252;ft.</p>
<p><strong>Varianten</strong></p>
<p>Folgende Varianten &#8211; bestimmt nicht vollst&#228;ndig &#8211; m&#252;ssen durch regul&#228;re Ausdr&#252;cke gefunden und ausgewertet werden.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">... &lt;form method=&quot;post&quot; action=&quot;mypermalinkurl&quot;&gt; ...
... &lt;form method='post' action='mypermalinkurl'&gt; ...
... &lt;form method=post action=mypermalinkurl&gt; ...
... &lt;form action=&quot;mypermalinkurl&quot; method=&quot;post&quot;&gt; ...
... &lt;form action='mypermalinkurl' method='post'&gt; ...
... &lt;form action=mypermalinkurl method=post&gt; ...
... &lt;form method=&quot;post&quot; action=&quot;mypermalinkurl&quot;&gt; &lt;input ...&gt;myinputfield&lt;/input&gt; ...</pre></td></tr></table></div>

<p><strong>Regex Form-Tag</strong></p>
<p>Regul&#228;rer Ausdruck wird auf dem &#252;bergebenen Ausgabepuffer angewendet.</p>
<p><em>Kurz notiert von links nach rechts.</em></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;form</pre></div></div>

<p>Findet genau die angegebene Zeichenketten.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;form.+method=</pre></div></div>

<p>Das Ergebnis wird auf viele Zeichen bis zum Ende der Zeichenkette <strong>method=</strong> erweitert. <strong>.</strong> f&#252;r ein beliebiges Zeichen und <strong>+</strong> (Wiederholungsfaktor: min. 1) f&#252;r mindestens eins oder mehrere des vorhergenden regul&#228;ren Ausdrucks (beliebiges Zeichen, der Punkt <strong>.</strong>).</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;form.+method=[&quot;']?post</pre></div></div>

<p>Der Ausdruck wird durch die Zeichen in eckigen Klammern und <strong>?</strong> (Wiederholungsfaktor: optional) zus&#228;tzlich der Zeichenkette <strong>post</strong> um optional ein <strong>&#8220;</strong> oder ein <strong>&#8216;</strong> erweitert, um die unterschiedlichen Notationen abzudecken (siehe Varianten Bsp. Unterschiede Zeile 1 <strong>&#8220;</strong>, 2 <strong>&#8216;</strong> und 3).</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;form.+method=[&quot;']?post.*?&gt;</pre></div></div>

<p>Das Form-Tag &#8211; <strong>nicht mehr und nicht weniger</strong> &#8211; wird ben&#246;tigt. Durch die letzte Erweiterung beliebiges Zeichen <strong>.</strong> in beliebiger Anzahl <strong>*</strong> (Wiederholungsfaktor: eins, viele oder keins) bis zum abschlie&#223;enden Zeichen <strong>&gt;</strong>, wird das Tag Form im ganzen gefunden. </p>
<p>Nicht <strong>mehr</strong> durch das letzte <strong>?</strong> (non-greedy). Ohne das Fragezeichen hinter dem Wiederholungsfaktor w&#252;rde das Ergebnis bis zur letzten &gt; des Ausgabepuffers reichen &#8211; etwas viel (siehe Bsp. Variante Zeile 7). </p>
<p><em>Die Wiederholungsfaktoren sind erst einmal &#8220;gierig&#8221; (greedy) &#8211; bis zum letzten und durch die zus&#228;tzliche Angabe <strong>?</strong> wird der Ausdruck &#8220;gen&#252;gsam&#8221; (non-greedy) &#8211; bis zum ersten aufgel&#246;st.</em></p>
<p><strong>Regex Action-URL</strong></p>
<p>Regul&#228;rer Ausdruck wird auf das Ergebnis von <strong>Regex Form-Tag</strong> angewendet.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">.*action=[&quot;'](.*?)[&quot;' &gt;]</pre></div></div>

<p>Referenzierung auf die URL durch die Klammerung. Die URL wird im zweiten Schritt ausgewertet, da die Reihenfolge der Attribute im Form-Tag nicht bekannt ist (siehe Bsp. Variante Zeile 1-3 und 4-6).</p>
<p><em>Gut, mit Look-around assertions h&#228;tte man bestimmt in einem Schritt auch die URL ermitteln k&#246;nnen, aber wollen ja auch nicht &#252;bertreiben.</em></p>
<p><strong>Regex Non-Permalink</strong></p>
<p>Regul&#228;rer Ausdruck wird auf das Ergebnis von <strong>Regex Action-URL</strong> angewendet.</p>
<p><em>Wie der Webserver eingehende Anfragen auswertet, kann au&#223;erhalb der Umgebung nicht f&#252;r alle F&#228;lle vorhergesagt werden. </em></p>
<p>Der folgende Ausdruck soll die URL zur weitere Verarbeitung auswerten. Endet die URL auf <strong>.php</strong> ist die Zieladresse bestimmt kein Permalink und das Formular muss nicht ber&#252;cksichtigt werden.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">.*\.php$</pre></div></div>

<p><strong>$</strong> kennzeichnet das Ende der Zeichenkette und \ hebt die Metabedeutung des n&#228;chsten Zeichens <strong>.</strong> auf.</p>
<p><strong>weiter &#8230;</strong></p>
<p>Die Konfigurationswerte f&#252;r die regul&#228;ren Ausdr&#252;cken k&#246;nnen im Administrationsbereich auf der Seite Einstellungen / Verschiedenes abgelegt werden.</p>
<p><img src="http://mgsimon.de/wp-content/uploads/2009/03/mg404rewrite_settings_misc_vals.png" alt="mg404rewrite_settings_misc_vals" title="mg404rewrite_settings_misc_vals" width="450" height="166" class="alignnone size-full wp-image-782 stamp" /></p>
<p><em>Apropos Konfiguration: Bin gleich &#252;ber einen Fehler gestolpert. Die Werte wurden nicht richtig dargestellt. Das Zeichen &#8221; hatte den Wert der Eigenschaft value vorzeitig beendet. Die in der Form darzustellenden Werte einfach zuvor mit der WordPress-Funktion attribute_escape() anpassen.</em></p>
<p>Soviel erst einmal zu den regul&#228;ren Ausdr&#252;cken. Das Plugin muss <em>nur</em> noch erweitert werden, damit alle Post-Forms an Permalinks verarbeitet werden k&#246;nnen. Dazu ein anderes mal mehr &#8230; :-) </p>
]]></content:encoded>
			<wfw:commentRss>http://mgsimon.de/2009/03/12/adwz-wp-plugin-17-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
