<?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>Tower Website Designs &#187; Technical</title>
	<atom:link href="http://www.towerdesigns.co.uk/topics/articles/technical/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.towerdesigns.co.uk</link>
	<description>Web Design &#38; Development</description>
	<lastBuildDate>Mon, 18 Feb 2013 13:39:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>The Regular Expressions</title>
		<link>http://www.towerdesigns.co.uk/2012/04/04/the-regular-expressions/</link>
		<comments>http://www.towerdesigns.co.uk/2012/04/04/the-regular-expressions/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 11:41:17 +0000</pubDate>
		<dc:creator>Matthew Spencer</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[aptana]]></category>
		<category><![CDATA[Code-Snippet]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.towerdesigns.co.uk/?p=157</guid>
		<description><![CDATA[Only just today have I realised the potential of Regular Expression &#8211; I&#8217;m no stranger to it, not by any means. I&#8217;ve used it for the usual search and replace, or finding something. But to my knowledge I have never used it in this capacity. I hope this article proves informative, regular expressions are worth learning [...]]]></description>
				<content:encoded><![CDATA[<p>Only just today have I realised the potential of Regular Expression &#8211; I&#8217;m no stranger to it, not by any means. I&#8217;ve used it for the usual search and replace, or finding something. But to my knowledge I have never used it in this capacity. I hope this article proves informative, regular expressions are worth learning about and getting familiar with, and this scenario has made me a fan for good.</p>
<p><span id="more-157"></span></p>
<p>My scenario is thus: I have written a number of lines of code, all using the same function and I&#8217;ve written them all the same with very little variability. For those curious I&#8217;m using PHP&#8217;s filter_var() function&#8230; which when I typed/copied/pasted these I was thinking in the same mindframe as using filter_input().</p>
<p>So my code looks a bit like this:</p>
<pre class="brush: php; title: ; notranslate">

$v_name  =     filter_var('v_name', FILTER_SANITIZE_STRING);
$v_email =     filter_var('v_email', FILTER_VALIDATE_EMAIL|FILTER_SANITIZE_EMAIL);
$v_phone =     filter_var('v_phone', FILTER_SANITIZE_STRING);

</pre>
<p>As I&#8217;m pottering away today, I decide to myself &#8211; knowing this code could be wrong&#8230; to check the PHP documentation. Indeed, to little surprise it should infact be the variable instead of a string. No biggie, except by now these function calls are scattered throughout a page and in quite the number.</p>
<p>I decide to use regular expression, or RegEx. This should allow me to match specifics (like filter_var), and if I can find the right resources, store a specific part &#8211; that part being the actual name of the variable, remove the single quotes (by the power of ignoring) and replace them with a dollar sign.</p>
<p>Cue some Google-fu, I turned to the usual site <a href="http://www.regular-expressions.info/">regular-expression.info</a> though it proves a bit flowery in language and wide (or specific) in scope; I&#8217;m hoping at this stage my choice of Aptana doesn&#8217;t hinder me. The <a title="Regular expression - wikipedia" href="http://en.wikipedia.org/wiki/Regular_expression">wikipedia article</a> is also feeling a bit obscure too. What I am looking for at this stage is how to store a specific match as a variable&#8230; and if regex even has this feature.</p>
<p>Finally, I <a title="O'Reilly - Summary of Regular Expressions" href="http://oreilly.com/openbook/cgi/appb_01.html">strike gold</a> &#8211; which states:</p>
<blockquote><dl>
<dt><tt>/(abc)/</tt></dt>
<dd>Matches <em>abc</em> anywhere within the string, but the parentheses act as memory, storing <em>abc</em> in the variable $1.</dd>
</dl>
</blockquote>
<p>What this means for me is that its possible, and I have to hope Aptana doesn&#8217;t have something I don&#8217;t know about which complicates matters. After a brief spot of experimenting (incidentally, <a title="GSkinner RegExr" href="http://www.gskinner.com/RegExr/">GSkinner&#8217;s RegExr</a> is a fantastic tool for this) I&#8217;m well on my way to tackling this.</p>
<h2>Solution</h2>
<p>Bare in mind that its worth making sure your regex works before committing to replacing; my find code eventually looked like this:</p>
<pre class="brush: plain; title: ; notranslate">

filter_var\('([a-z_]+)'

</pre>
<p>To break this down:</p>
<ul>
<li>filter_var\(&#8216;&#8230;&#8217;</li>
</ul>
<p>This is the static part of the string, the function filter_var(, and the quotes &#8221; the backslash &#8216;escapes&#8217; the opening bracket (so its a part of what you&#8217;re looking for).</p>
<ul>
<li>([a-z_]+)</li>
</ul>
<p>This is looking for characters within the range a-z, and underscores. Storing it using the parenthesis.</p>
<ul>
<li>[]+</li>
</ul>
<p>The square brackets surround a group (above being a-z_), the + sign means multiples of the preceding group.</p>
<p>After making sure it works as expected, you want to replace (&#8216;&#8230;&#8217; with ($&#8230; &#8211; the code for replace is far simpler in my opinion:</p>
<pre class="brush: plain; title: ; notranslate">

filter_var\(\$$1

</pre>
<p>The backslashes you see dotted around &#8216;escape&#8217; the following character, regular expression uses some characters, so needs to know when they&#8217;re a part of the expression, or a part of the string. Hence why the first dollar sign and opening parenthesis are escaped. Note the lack of single-quotes in the replacement expression and their presence in the find string.</p>
<p>If all goes well, the faulty lines of code are all replaced and should look something like this:</p>
<pre class="brush: php; title: ; notranslate">
$v_name  =     filter_var($v_name, FILTER_SANITIZE_STRING);
$v_email =     filter_var($v_email, FILTER_VALIDATE_EMAIL|FILTER_SANITIZE_EMAIL);
$v_phone =     filter_var($v_phone, FILTER_SANITIZE_STRING);&lt;/pre&gt;
</pre>
<p>Mine certainly did the trick!</p>
<h2>Conclusion</h2>
<p>While its debatable that it saved me much time right now thanks to the need for research, I&#8217;ve written this as a reminder to myself, as well as hopefully helping anyone else who might want to find out if regex can store matches as variables; or indeed the curious programmer who may not be aware of Regular Expression and its potential power.</p>
<p>In the future this kind of knowledge could save me a headache, and maybe even a good 20 minutes of repetitive correction; so I hope it helps you too &#8211; maybe not even for programming, most decent text editors have regular expressions as an option when finding things in a file (usually under advanced options).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.towerdesigns.co.uk/2012/04/04/the-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
