<?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; xkcd</title>
	<atom:link href="http://magp.ie/tag/xkcd/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>Tue, 31 Jan 2012 19:01:48 +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; xkcd</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>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&amp;blog=11708208&amp;post=28&amp;subd=blogalhost&amp;ref=&amp;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&amp;blog=11708208&amp;post=28&amp;subd=blogalhost&amp;ref=&amp;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>
