<?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>Lateral Code &#187; perl</title>
	<atom:link href="http://www.lateralcode.com/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lateralcode.com</link>
	<description>A Web Development Blog Focused on Code and Technology</description>
	<lastBuildDate>Thu, 26 Aug 2010 22:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Regular Expressions</title>
		<link>http://www.lateralcode.com/regular-expressions/</link>
		<comments>http://www.lateralcode.com/regular-expressions/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 06:50:44 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=853</guid>
		<description><![CDATA[How many times have you had to dig through a long, confusing string for information? This has been a common problem for years. What&#8217;s the solution? Regular expressions. What are regular expressions? Well.. Now that you&#8217;ve seen that&#8230; guess why so many people do NOT program in Perl? To put it very simply, regular expressions [...]]]></description>
			<content:encoded><![CDATA[<p>How many times have you had to dig through a long, confusing string for information? This has been a common problem for years. What&#8217;s the solution? Regular expressions.</p>
<p>What are regular expressions? Well..</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads/regex.png" class="aligncenter" alt="Long And Complex Regular Expression"/></p>
<p>Now that you&#8217;ve seen that&#8230; guess why so many people do NOT program in Perl? <img src='http://www.lateralcode.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>To put it very simply, regular expressions are strings that search for patterns. Patterns can be a few letters or enormous strings with many wild cards. In this tutorial, I will outline some basics of regular expressions (also known as regexes).</p>
<p><span id="more-853"></span></p>
<p>Before I get started, I must tell you that what you saw before is an extremely complicated regular expressions that even I do not understand fully. Please do not get discouraged because of it. Perl is an excellent language that I recently used to create <a href="http://twitter.lateralcode.com">my Twitter application</a>. I guarantee this article will be easy to follow!</p>
<p>All regexes must contain two similar start and end characters. Extra characters at the end are referred to as modifiers. Those that are at the beginning are called operators. In the middle lies the real pattern. </p>
<p>This real pattern contains everything that should be matched. For example, if you wanted to match the pattern &#8220;ab&#8221; in Perl, use:</p>
<pre lang="perl">$str =~ m/ab/</code></pre>
<p>Notice the &#8220;m&#8221; operator. It is one of the most common operators in Perl, and it is is used for simple matching. If no operator is specified, the match operator is used by default.</p>
<p>In the above case, / is our start and end character. We could have very well used:</p>
<pre lang="perl">$str =~ m%ab%</code></pre>
<p>Be careful! Regular Expressions are <em>case sensitive</em>. If you were actually trying to match &#8220;AB&#8221;, the above pattern would not work. To make any regex case insensitive, use the &#8220;i&#8221; modifier:</p>
<pre lang="perl">$str =~ m/ab/i</code></pre>
<p>Well, now that you&#8217;ve seen two simple examples, you are probably wondering how to use them. In Perl, it&#8217;s as easy as placing them in an if statement:</p>
<pre lang="perl">if( $my_str =~ m/ab/i ) {
	# The code to execute assuming "ab" is in $my_str
}</code></pre>
<p>Matching returns true if the regular expression finds a match. Otherwise, it comes up as false.</p>
<p>How about something a bit more advanced? Let&#8217;s say you wanted to match &#8220;cat&#8221;:</p>
<pre lang="perl">$string =~ m/cat/</code></pre>
<p>But wait! What if you don&#8217;t mind &#8220;cat&#8221;, &#8220;hat&#8221;, or &#8220;bat&#8221;? You can use () and | to represent &#8220;or&#8221;:</p>
<pre lang="perl">$string =~ m/(c|h|b)at/</code></pre>
<p>Between each | lies a possible pattern to match. Now, let&#8217;s say you wanted to match &#8220;him&#8221; or &#8220;Robert&#8221;:</p>
<pre lang="perl">$string =~ m/(him|Robert)/</code></pre>
<p>It&#8217;s very simple! For more practice, repeat this process with &#8220;child&#8221; and &#8220;children&#8221;:</p>
<pre lang="perl">$info =~ m/(child|children)/</code></pre>
<p>Now, take a look at that pattern. Doesn&#8217;t it seem a bit redundant? You are matching the word &#8220;child&#8221; in both &#8220;child&#8221; and &#8220;children&#8221;. Shouldn&#8217;t there be a way to make part of a regular expression optional? Well, there is! The &#8220;?&#8221; can be used in a regular expression to make the preceding character(s) optional:</p>
<pre lang="perl">$info =~ m/child(ren)?/</code></pre>
<p>If you are just making one character optional, you can omit the parentheses:</p>
<pre lang="perl">$var =~ m/favou?rite/</code></pre>
<p>The above pattern will match &#8220;favorite&#8221; or &#8220;favourite&#8221;. To finish up this lesson on regular expressions, let&#8217;s go through how to get the actual match from a string.</p>
<p>Patterns that are enclosed by parentheses may be accessed using the variables $1 &#8230; $9 with Perl. For example:</p>
<pre lang="perl">if( $my_var =~ m/(cat)/ ) {
	print $1; # This will print "cat"
}</code></pre>
<p>But why would you want to do this? You already know the match is going to be &#8220;cat&#8221;. Parentheses are mainly used when certain things are optional, or many different matches may occur:</p>
<pre lang="perl">if( $my_var =~ m/(honou?r)/ ) {
	print $1; # This will print either "honor" or "honour", depending on which was matched
}</code></pre>
<p>Remember that there are different variables &#8211; $1 &#8230; $9 &#8211; to represent these matches. Each variable represents a pattern enclosed in parentheses. The order of the variables matches the order of parentheses in the pattern:</p>
<pre lang="perl">if( $some_str =~ m/((r|m|s)at)/ ) {
	print $1; # This will print "rat", "mat", or "sat"
	print $2; # This will print "r", "m", or "s"
}</code></pre>
<p>To make everything a bit easier, Perl also returns the complete matched string through $&#038;. So:</p>
<pre lang="perl">if( $some_info =~ m/hello/ ) {
	print $&#038;; # This will print "hello"
}</code></pre>
<p>is an alternative (and easier) way to do this:</p>
<pre lang="perl">if( $some_info =~ m/(hello)/ {
	print $1; # This will also print "hello"
}</code></pre>
<p>That&#8217;s all there is for today! There will soon be more in this series. Thank you for reading!<!--noadsense--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/regular-expressions/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
