<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Magp.ie &#187; regex</title>
	<atom:link href="http://magp.ie/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://magp.ie</link>
	<description>A nest for the random, shiny, online tidbits I stumble across...</description>
	<lastBuildDate>Wed, 16 May 2012 09:40:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='magp.ie' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/061e340c5da13b5a41ae8016bee03aa8?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Magp.ie &#187; regex</title>
		<link>http://magp.ie</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://magp.ie/osd.xml" title="Magp.ie" />
	<atom:link rel='hub' href='http://magp.ie/?pushpress=hub'/>
		<item>
		<title>Faster Javascript Trim</title>
		<link>http://magp.ie/2011/07/28/faster-javascript-trim/</link>
		<comments>http://magp.ie/2011/07/28/faster-javascript-trim/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 21:25:02 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[fast]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[trim]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=592</guid>
		<description><![CDATA[Want the best way to trim a sting in javascript? A while ago, I came across a post on such a subject. It was written a few years ago but it&#8217;s still as relevant and useful as ever. http://blog.stevenlevithan.com/archives/faster-trim-javascript  The &#8230; <a href="http://magp.ie/2011/07/28/faster-javascript-trim/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=592&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Want the best way to trim a sting in javascript?</p>
<p>A while ago, I came across a post on such a subject. It was written a few years ago but it&#8217;s still as relevant and useful as ever.</p>
<p><span id="more-592"></span></p>
<p><a title="faster-trim-javascript " href="http://blog.stevenlevithan.com/archives/faster-trim-javascript" target="_blank">http://blog.stevenlevithan.com/archives/faster-trim-javascript </a></p>
<p>The author basically compares a bunch of different methods to trim a string across the 2 big browsers at the time, Internet Explorer and Firefox, and figures out the fastest way of doing it.</p>
<p>For more detail, follow the link above, otherwise, here is the conclusion.</p>
<p><strong>Fastest trim method for normal length strings</strong></p>
<p><pre class="brush: jscript;">
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
</pre></p>
<p><strong>Fastest trim method for super long strings</strong></p>
<p><pre class="brush: jscript;">
function trim11 (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i &gt;= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
</pre></p>
<p><strong>Note</strong>: For the super geeks out there, <a title="Trim testing" href="http://jsperf.com/mega-trim-test" target="_blank">this site will show you and allow you to test what is the fastest trim method on a particular browser&#8230; enjoy</a>!</p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/fast/'>fast</a>, <a href='http://magp.ie/tag/javascript/'>javascript</a>, <a href='http://magp.ie/tag/optimize/'>optimize</a>, <a href='http://magp.ie/tag/regex/'>regex</a>, <a href='http://magp.ie/tag/trim/'>trim</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/592/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/592/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/592/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/592/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/592/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/592/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/592/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/592/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/592/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/592/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/592/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/592/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/592/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/592/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=592&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2011/07/28/faster-javascript-trim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
		<item>
		<title>Regular expression to remove excess whitespace</title>
		<link>http://magp.ie/2011/01/30/regular-expression-to-remove-excess-whitespace/</link>
		<comments>http://magp.ie/2011/01/30/regular-expression-to-remove-excess-whitespace/#comments</comments>
		<pubDate>Sun, 30 Jan 2011 15:11:38 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[tabs]]></category>
		<category><![CDATA[whitespace]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=503</guid>
		<description><![CDATA[Neat way of removing excess whitespace and tabbing from strings. Filed under: Code Tagged: php, regex, strings, tabs, whitespace<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=503&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Neat way of removing excess whitespace and tabbing from strings.</p>
<p><pre class="brush: php;">
$string = &quot;Tom\tThumb\t                is sooooooo\t\t\tdumb&quot;;
$string = preg_replace( '/\s+/', ' ', $string );
echo $string;
//will echo &quot;Tom Thumb is sooooooo dumb&quot;
</pre></p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/php/'>php</a>, <a href='http://magp.ie/tag/regex/'>regex</a>, <a href='http://magp.ie/tag/strings/'>strings</a>, <a href='http://magp.ie/tag/tabs/'>tabs</a>, <a href='http://magp.ie/tag/whitespace/'>whitespace</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/503/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/503/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/503/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/503/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/503/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/503/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/503/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/503/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/503/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/503/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/503/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/503/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/503/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/503/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=503&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2011/01/30/regular-expression-to-remove-excess-whitespace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
		<item>
		<title>Addslashes and stripslashes in Javascript</title>
		<link>http://magp.ie/2011/01/20/addslashes-and-stripslashes-in-javascript/</link>
		<comments>http://magp.ie/2011/01/20/addslashes-and-stripslashes-in-javascript/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 17:13:05 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[escape]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=485</guid>
		<description><![CDATA[Simple one. If you need to add/remove escape slashes to/from a string, use the following code. Source &#8211; http://javascript.about.com/library/bladdslash.htm Filed under: Code Tagged: escape, javascript, regex<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=485&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Simple one. If you need to add/remove <a href="http://en.wikipedia.org/wiki/Escape_character">escape slashes</a> to/from a string, use the following code.<br />
<span id="more-485"></span><br />
<pre class="brush: php;">
function addslashes(str) {
    str = str.replace(/\\/g, '\\\\');
    str = str.replace(/\'/g, '\\\'');
    str = str.replace(/\&quot;/g, '\\&quot;');
    str = str.replace(/&#092;&#048;/g, '\&#092;&#048;');
    return str;
}

function stripslashes(str) {
    str = str.replace(/\\'/g, '\'');
    str = str.replace(/\\&quot;/g, '&quot;');
    str = str.replace(/\&#092;&#048;/g, '&#092;&#048;');
    str = str.replace(/\\\\/g, '\\');
    return str;
}
</pre></p>
<p>Source &#8211; <a href="http://javascript.about.com/library/bladdslash.htm">http://javascript.about.com/library/bladdslash.htm</a></p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/escape/'>escape</a>, <a href='http://magp.ie/tag/javascript/'>javascript</a>, <a href='http://magp.ie/tag/regex/'>regex</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/485/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=485&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2011/01/20/addslashes-and-stripslashes-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
		<item>
		<title>Remove non-UTF8 characters from string with PHP</title>
		<link>http://magp.ie/2011/01/06/remove-non-utf8-characters-from-string-with-php/</link>
		<comments>http://magp.ie/2011/01/06/remove-non-utf8-characters-from-string-with-php/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 21:58:22 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[UTF8]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=447</guid>
		<description><![CDATA[If you have come across the cursed &#8216;Invalid Character&#8216; error while using PHP&#8217;s XML or JSON parser then you may be interested in this. Unfortunately, PHP&#8217;s XML and JSON parsers do not ignore non-UTF8 characters, but rather they stop and &#8230; <a href="http://magp.ie/2011/01/06/remove-non-utf8-characters-from-string-with-php/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=447&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you have come across the cursed &#8216;<a href="http://petewarden.typepad.com/searchbrowser/2008/04/illegal-charact.html">Invalid Character</a>&#8216; error while using PHP&#8217;s <a href="http://ie2.php.net/manual/en/book.xml.php">XML</a> or <a href="http://www.php.net/manual/en/book.json.php">JSON</a> parser then you may be interested in this.<br />
<span id="more-447"></span><br />
Unfortunately, PHP&#8217;s XML and JSON parsers do not ignore non-UTF8 characters, but rather they stop and throw a rather unhelpful error. I found a number of solutions to this that did not work for me, namely using <a href="http://www.php.net/manual/en/function.iconv.php">iconv</a> and <a href="http://www.php.net/manual/en/function.utf8-encode.php">utf8_encode</a>.</p>
<p>Then I found this <a href="http://webcollab.sourceforge.net/unicode.html">excellent explanation</a> of using UTF8 with PHP, which is well worth a read.</p>
<p>Encoding gives me a headache but from this explanation this is how I see it. </p>
<p>I had some character that the parser does not know how to interput because it was outside the byte range of the <a href="http://en.wikipedia.org/wiki/UTF-8">UTF8</a> format. Some of the PHP functions, like iconv, still let some non-UTF8 characters through which breaks the parser. The <a href="http://www.php.net/manual/en/function.preg-replace.php">preg_replace</a> just rips out any non-UTF8 character based on it&#8217;s byte sequence and replaces it with a question mark. </p>
<p>From that article above, I use the following code to remove any non-UTF8 characters.</p>
<p><pre class="brush: php;">
//reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ?
$some_string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
 '|[\x00-\x7F][\x80-\xBF]+'.
 '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
 '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
 '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
 '?', $some_string );

//reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
$some_string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'.
 '|\xED[\xA0-\xBF][\x80-\xBF]/S','?', $some_string );
</pre></p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/encoding/'>encoding</a>, <a href='http://magp.ie/tag/php/'>php</a>, <a href='http://magp.ie/tag/regex/'>regex</a>, <a href='http://magp.ie/tag/utf8/'>UTF8</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/447/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/447/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/447/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/447/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/447/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/447/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/447/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/447/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/447/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/447/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/447/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/447/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/447/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/447/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=447&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2011/01/06/remove-non-utf8-characters-from-string-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
		<item>
		<title>Regular Expressions</title>
		<link>http://magp.ie/2010/03/08/regular-expressions-email-url-parse-assignment/</link>
		<comments>http://magp.ie/2010/03/08/regular-expressions-email-url-parse-assignment/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 22:16:08 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[parse assignment value]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[validate email]]></category>
		<category><![CDATA[validate url]]></category>
		<category><![CDATA[xkcd]]></category>

		<guid isPermaLink="false">http://blogalhost.wordpress.com/?p=28</guid>
		<description><![CDATA[First off, regular expressions are great. They are a handy quick way to validate or parse data and you can use them in almost all languages. But of all things, I forget neat regex&#8217;s and in fairness they are a &#8230; <a href="http://magp.ie/2010/03/08/regular-expressions-email-url-parse-assignment/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=28&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>First off, regular expressions are great.</p>
<p>They are a handy quick way to validate or parse data and you can use them in almost all languages. But of all things, I forget neat regex&#8217;s and in fairness they are a pita to recall as the syntax is plain nutty.</p>
<p>This is where it ends. I am going to reference all the neat regex&#8217;s in this blog as I come across them rather than rely on mother Google.<br />
<span id="more-28"></span><br />
Starting with a couple of simple functions I use to validate email addresses (thanks WordPress core!) and URLs.<br />
<pre class="brush: php;">
function is_url ( $url ) {
	return ( ! preg_match ( '/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url ) ) ? FALSE : TRUE;
}

function is_email( $email ) {
	// Test for the minimum length the email can be
	if ( strlen( $email ) &lt; 3 ) {
		return false;
	}

	// Test for an @ character after the first position
	if ( strpos( $email, '@', 1 ) === false ) {
		return false;
	}

	// Split out the local and domain parts
	list( $local, $domain ) = explode( '@', $email, 2 );

	// LOCAL PART
	// Test for invalid characters
	if ( !preg_match( '/^[a-zA-Z0-9!#$%&amp;\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
		return false;
	}

	// DOMAIN PART
	// Test for sequences of periods
	if ( preg_match( '/\.{2,}/', $domain ) ) {
		return false;
	}

	// Test for leading and trailing periods and whitespace
	if ( trim( $domain, &quot; \t\n\r&#092;&#048;\x0B.&quot; ) !== $domain ) {
		return false;
	}

	// Split the domain into subs
	$subs = explode( '.', $domain );

	// Assume the domain will have at least two subs
	if ( 2 &gt; count( $subs ) ) {
		return false;
	}

	// Loop through each sub
	foreach ( $subs as $sub ) {
		// Test for leading and trailing hyphens and whitespace
		if ( trim( $sub, &quot; \t\n\r&#092;&#048;\x0B-&quot; ) !== $sub ) {
			return false;
		}

		// Test for invalid characters
		if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
			return false;
		}
	}

	// Congratulations your email made it!
	return true;
}
</pre></p>
<p>Lastly and most recently, I wanted to parse a string of code to find an assignment value.<br />
I came across a neat regex to help me parse out the values of these variables. </p>
<p><pre class="brush: php;">
$code = &quot;var string_variable = Superduper;
var digit_variable = 123456;&quot;;

function get_assignment_value( $needle, $haystack, $type = 'string' ) {
	if( $type == 'digit' )
		preg_match( '/.'.$needle.' = (?P&lt;value&gt;\d+)/', $haystack, $matches );
	else
		preg_match( '/.'.$needle.' = (?P&lt;value&gt;\w+)/', $haystack, $matches );
	
	if( empty( $matches[ 'value' ] ) )
		return false;

	if( $type == 'digit' )
		return (int) $matches[ 'value' ];
	
	return $matches[ 'value' ];
}

var_dump( get_assignment_value( 'string_variable', $code ) ); // string(10) &quot;Superduper&quot;
var_dump( get_assignment_value( 'digit_variable', $code, 'digit' ) ); // int(123456) 
var_dump( get_assignment_value( 'digit_variable', $code  ) ); // string(6) &quot;123456&quot; 
</pre></p>
<p>My main source of guidance on this voodoo <a title="Guide to regexs" href="http://www.regular-expressions.info/" target="_blank">here</a> and their <a href="http://www.regular-expressions.info/reference.html">sometimes hard to find but useful reference</a>.</p>
<p>Also, <a href="http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html" target="_blank">here</a> is a good starter on building a regex.</p>
<p><img class="alignnone" title="Wait, forgot to escape a space.  Wheeeeee[taptaptap]eeeeee." src="http://imgs.xkcd.com/comics/regular_expressions.png" alt="I know regular expressions" width="600" height="607" /></p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/parse-assignment-value/'>parse assignment value</a>, <a href='http://magp.ie/tag/regex/'>regex</a>, <a href='http://magp.ie/tag/regular-expression/'>regular expression</a>, <a href='http://magp.ie/tag/validate-email/'>validate email</a>, <a href='http://magp.ie/tag/validate-url/'>validate url</a>, <a href='http://magp.ie/tag/xkcd/'>xkcd</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=28&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2010/03/08/regular-expressions-email-url-parse-assignment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>

		<media:content url="http://imgs.xkcd.com/comics/regular_expressions.png" medium="image">
			<media:title type="html">Wait, forgot to escape a space.  Wheeeeee[taptaptap]eeeeee.</media:title>
		</media:content>
	</item>
	</channel>
</rss>
