<?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>Chris Tate-Davies &#187; regex</title>
	<atom:link href="http://www.christatedavies.co.uk/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christatedavies.co.uk</link>
	<description>An archive of helpful tit bits of information for development, and probably some stuff that is incomplete, wrong or boring...</description>
	<lastBuildDate>Tue, 31 Jan 2012 11:18:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Retrieve email using regex</title>
		<link>http://www.christatedavies.co.uk/2010/06/23/retrieve-email-using-regex/</link>
		<comments>http://www.christatedavies.co.uk/2010/06/23/retrieve-email-using-regex/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 09:45:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=90</guid>
		<description><![CDATA[This horrendous regular expression will parse a string and return a valid email address from it. $email = "&#60;'Freddy'&#62; fred@live.com"; preg_match('/[a-z0-9!#$%&#38;'*+/=?^_`{&#124;}~-]+(?:\.[a-z0-9!#$%&#38;'*+/=?^_`{&#124;}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/', $email, $match); echo $match[0]; This will return: fred@live.com Basically, if you pass an array as the third parameter of the preg_match method, it will be filled with the preg_match results, and the first item of the [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/06/23/retrieve-email-using-regex/' addthis:title='Retrieve email using regex ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This horrendous regular expression will parse a string and return a valid email address from it.</p>
<pre>$email = "&lt;'Freddy'&gt; fred@live.com";</pre>
<pre>preg_match('/[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/', $email, $match);</pre>
<pre>echo $match[0];</pre>
<p>This will return:</p>
<pre>fred@live.com</pre>
<p>Basically, if you pass an array as the third parameter of the preg_match method, it will be filled with the preg_match results, and the first item of the array will be the matching string. If you use capturing groups, these will also be filled. Read more about preg_match at the PHP site <a href="http://uk3.php.net/manual/en/function.preg-match.php" target="_blank">here</a>.</p>
<p>I am told that this expression will match 99.9% of valid email addresses in the wild.</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/06/23/retrieve-email-using-regex/' addthis:title='Retrieve email using regex ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/06/23/retrieve-email-using-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Online regex -&gt; php tool</title>
		<link>http://www.christatedavies.co.uk/2010/06/22/online-regex-php-tool/</link>
		<comments>http://www.christatedavies.co.uk/2010/06/22/online-regex-php-tool/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 15:43:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[online]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=87</guid>
		<description><![CDATA[I&#8217;ve found an excellent online tool, where you can give it a regex, and it will spit out the PHP syntax for use with preg_replace, preg_match, etc. I&#8217;m forever getting &#8216;Unknown modifier&#8217; errors when trying to insert regex&#8217;s into my PHP code. This sorts it out for you. http://regex.larsolavtorvik.com/<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/06/22/online-regex-php-tool/' addthis:title='Online regex -&#62; php tool ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found an excellent online tool, where you can give it a regex, and it will spit out the PHP syntax for use with preg_replace, preg_match, etc.</p>
<p>I&#8217;m forever getting &#8216;Unknown modifier&#8217; errors when trying to insert regex&#8217;s into my PHP code. This sorts it out for you.</p>
<p><a href="http://regex.larsolavtorvik.com/" target="_blank">http://regex.larsolavtorvik.com/</a></p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/06/22/online-regex-php-tool/' addthis:title='Online regex -&gt; php tool ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/06/22/online-regex-php-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regex &#8211; &#8220;The&#8221; searching</title>
		<link>http://www.christatedavies.co.uk/2010/03/26/regex-the-searching/</link>
		<comments>http://www.christatedavies.co.uk/2010/03/26/regex-the-searching/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 14:04:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=17</guid>
		<description><![CDATA[Say you have a list of movie titles, and you want to either sort them, or search through them, and some of them have &#8220;The &#8221; at the start, for example: The Simpsons Simpson Street When doing a MySQL search: SELECT * FROM movies WHERE title LIKE "The Simp%"; Would only return the first row. [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/26/regex-the-searching/' addthis:title='Regex &#8211; &#8220;The&#8221; searching ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Say you have a list of movie titles, and you want to either sort them, or search through them, and some of them have &#8220;The &#8221; at the start, for example:</p>
<ul>
<li>The Simpsons</li>
<li>Simpson Street</li>
</ul>
<p>When doing a MySQL search:</p>
<pre>SELECT * FROM movies WHERE title LIKE "The Simp%";</pre>
<p>Would only return the first row. But if you are working in a company where there is no standard set, the movie title could be formatted as &#8220;Simpsons, The&#8221; &#8211; and then, it won&#8217;t be found.</p>
<p>To solve this, you could replace the &#8220;The &#8221; letters with blank, and then sort out the field contents during the query:</p>
<pre>$str_query = preg_replace("/(title like "(the )(.*)%")/i",
    "REPLACE(LOWER(title), "the ", "") LIKE ("$3%")",
    $str_query);</pre>
<p>This will change :</p>
<pre>"(title LIKE "The Simpsons")"</pre>
<p>to,</p>
<pre>"(title LIKE "Simpsons")"</pre>
<p>But, the (the) in line 2 tells PHP to only replace it if starts with &#8220;The &#8221; (case insenstive).</p>
<p>However, what if you want to search for &#8220;the&#8221;  (not sure why you would&#8230;)</p>
<p>You need to do a negative lookahead, to tell the expression to only carry on, if the search phrase is not exactly &#8220;the&#8221;</p>
<pre>if (preg_match("/(title like "(?!the)(.*)%")/i", $str_query)) {</pre>
<p>The (?!the) is the readahead.</p>
<p>(.*) matches any string but it is greedy and you have to be carfeul that it doesn&#8217;t just accept everything to the end of $str_query. (but its okay in our case, as we are looking for % (the LIKE wildcard))</p>
<p>After all this, we can run:</p>
<pre>SELECT * FROM movies WHERE $str_query;</pre>
<p>But what about sorting? All the titles beginning with &#8220;The&#8221; will appear in the T section. Whereas really, we want the Simpsons to appear in the S section.</p>
<p>Add an easy ORDER BY clause here:</p>
<pre>SELECT * FROM movies WHERE $str_query ORDER BY (REPLACE(title, "the ", "") ASC;</pre>
<p>Sorted!</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/26/regex-the-searching/' addthis:title='Regex &#8211; &#8220;The&#8221; searching ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/03/26/regex-the-searching/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Online Regular Expression Testing Tool</title>
		<link>http://www.christatedavies.co.uk/2010/03/18/online-regular-expression-testing-tool/</link>
		<comments>http://www.christatedavies.co.uk/2010/03/18/online-regular-expression-testing-tool/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 00:16:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=5</guid>
		<description><![CDATA[Regular expressions &#8212; everyone loves them, right? Well, I am currently running Ubuntu, and I would normally use RegexBuddy &#8211; but cannot unless I use Wine, and I don&#8217;t really like to. So, I wanted to find an online solution for my needs. So, I found this one, that allows my to test my regular [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/online-regular-expression-testing-tool/' addthis:title='Online Regular Expression Testing Tool ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<h2><span style="line-height: 19px; font-size: 13px;">Regular expressions &#8212; everyone loves them, right?</span></h2>
<p>Well, I am currently running Ubuntu, and I would normally use RegexBuddy &#8211; but cannot unless I use Wine, and I don&#8217;t really like to. So, I wanted to find an online solution for my needs.</p>
<p>So, I found this one, that allows my to test my regular expressions quickly and simply.</p>
<p><a href="http://gskinner.com/RegExr" target="_blank">http://gskinner.com/RegExr/</a></p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/online-regular-expression-testing-tool/' addthis:title='Online Regular Expression Testing Tool ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/03/18/online-regular-expression-testing-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

