<?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"
	>

<channel>
	<title>Developer Tutorials' Webmaster Blog &#187; regular expressions</title>
	<atom:link href="http://www.developertutorials.com/blog/tag/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.developertutorials.com/blog</link>
	<description>Keeping webmasters up-to-date on technology.</description>
	<pubDate>Tue, 02 Sep 2008 14:59:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Learn regular expressions in PHP</title>
		<link>http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/</link>
		<comments>http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 11:00:04 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/</guid>
		<description><![CDATA[Love them or hate them, regular expressions are here to stay. When it comes to quickly dealing with large blocks of data, batch processing operations or screen scraping, regular expressions are often the most effective solution. There&#8217;s just one problem, though - learning them can be as hard as learning a new language altogether. Here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Love them or hate them, regular expressions are here to stay. When it comes to quickly dealing with large blocks of data, batch processing operations or screen scraping, regular expressions are often the most effective solution. There&#8217;s just one problem, though - learning them can be as hard as learning a new language altogether. Here&#8217;s how to get off to a flying start.</p>
<p><span id="more-152"></span></p>
<p>First, you need to how regular expressions work. Stick with PCRE - the preg_* functions - they&#8217;re faster, more reliable and expressions for them are more common. The different styles of regular expressions each define a mini language for matching patterns within a block of text. Screen scraping is probably the most popular example. Consider this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;http://example.com/1&quot;&gt;1&lt;/a&gt;
&lt;a href=&quot;http://example.com/2&quot;&gt;Not 1.&lt;/a&gt;
&lt;a href=&quot;http://example.com/?query#someid&quot;&gt;Yet another link.&lt;/a&gt;</pre></div></div>

<p>What if you wanted to programmatically extract the link URLs and text from each of these? At this level of simplicty, some basic explode()s might be enough, and when you need to be more tolerant for the data, XML parsing is probably the way to go. But for now, just looking at that, regular expressions would be the easiest way. And we can build a pattern that defines what is to be matched so that the regular expression engine (in this case, PCRE) will find all matches and give them to us.</p>
<p>One of the best ways to understand regular expressions in practise, I find, is to consider mod_rewrite. You&#8217;ve seen those fantastic URLs on websites - e.g. http://example.com/users/myusername - these don&#8217;t actually exist as a file, but we can use Apache&#8217;s mod_rewrite engine to &#8220;rewrite&#8221; these URLs to other URLs. We might make that /users/(some username) go to /users.php?username=(the username). The best tutorial for this, by far, is <a href="http://corz.org/serv/tricks/htaccess2.php" target="_blank">the corz.org htaccess tips and tricks</a> guide. Have a very thorough read through this and experiment with it extensively before you go on.</p>
<p>Next, get equipped to test out regular expressions. You&#8217;re probably using Firefox. If you aren&#8217;t, <a href="http://getfirefox.com/" target="_blank">get it</a>. Then get the <a href="http://sebastianzartner.de/firefoxExtensions/RExT/" target="_blank">Regular Expressions Tester Firefox extension</a>. This will be invaluable for experimenting with regular expressions - the best way to learn.</p>
<p>Of course, you&#8217;ll need to be familiar with the PHP functions that will provide your regular expression workhorse. Bookmark <a href="http://www.developertutorials.com/php-manual/function.preg-match.html" target="_blank">the preg_match function&#8217;s manual entry</a> and refer back to it as you need to. The manual page has some invaluable examples which you should read through extensively.</p>
<p>Finally, when in doubt, keep a cheatsheet at hand. <a href="http://www.ilovejackdaniels.com/cheat-sheets/regular-expressions-cheat-sheet/">ILoveJackDaniels.com</a> has a fantastic <a href="http://www.ilovejackdaniels.com/regular_expressions_cheat_sheet.png" target="_blank">regular expression cheatsheet</a> that you should also bookmark.</p>
<p>With all these ready, you&#8217;ll be off to a great start. Remember, the best way to learn is to practise &#8212; read through real, working code, then write your own. Once you&#8217;ve finished all this, you&#8217;ll be a regular expression whiz in no time.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark" style="float:left;">
<div class="d152" style="overflow:hidden">
<div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/&amp;title=Learn+regular+expressions+in+PHP" title="Add to&nbsp;Del.icio.us">Del.icio.us</a></div><div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/&amp;title=Learn+regular+expressions+in+PHP" title="Add to&nbsp;Digg This">Digg This</a></div><div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/&amp;title=Learn+regular+expressions+in+PHP" title="Add to&nbsp;Stumble">Stumble</a></div></div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
