<?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>sevenson.com.au &#187; Flex</title>
	<atom:link href="http://www.sevenson.com.au/tag/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sevenson.com.au</link>
	<description>The online presence of Andrew Sevenson</description>
	<lastBuildDate>Fri, 06 Aug 2010 12:40:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Testing Regular Expressions in ActionScript 3</title>
		<link>http://www.sevenson.com.au/actionscript/testing-regular-expressions/</link>
		<comments>http://www.sevenson.com.au/actionscript/testing-regular-expressions/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 10:21:17 +0000</pubDate>
		<dc:creator>Andrew Sevenson</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.sevenson.com.au/?p=368</guid>
		<description><![CDATA[I&#8217;m by no means an expert on Regular Expressions. I mean, I get what they do, and can see their awesomeness, but damn they do my head in sometimes. To help myself out, I wrote a quick little Flex app that lets you test how a regular expression will work with a particular string. It [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.sevenson.com.au/content/uploads/2009/09/icon2.jpg"><img src="http://www.sevenson.com.au/content/uploads/2009/09/icon2.jpg" alt="" title="Regular Expression Icon" width="250" height="190" class="alignright size-full wp-image-540" /></a>I&#8217;m by no means an expert on Regular Expressions.  I mean, I get what they do, and can see their awesomeness, but damn they do my head in sometimes.</p>
<p>To help myself out, I wrote a quick little Flex app that lets you test how a regular expression will work with a particular string.  It lets you try out all the Flash Regular Expression methods and see each of the results.</p>
<p>To top it all off, I put together a quick overview of the different Flash Regular Expression methods that are available, as well as a short overview of the Regular Expression syntax.</p>
<p>You can check it all out right here&#8230;</p>
<p><span id="more-368"></span><br />
<a name="flashdemo"></a>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_swfloader_254609433"
			class="flashmovie"
			width="650"
			height="400">
	<param name="movie" value="http://www.sevenson.com.au/content/common/swfloader.swf" />
	<param name="flashvars" value="contentURL=http://www.sevenson.com.au/content/actionscript/regexp/regexp.swf&amp;previewURL=http://www.sevenson.com.au/content/actionscript/regexp/preview.jpg&amp;clickToLoad=true&amp;corners=''" />
	<param name="quality" value="high" />
	<param name="wmode" value="transparent" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.sevenson.com.au/content/common/swfloader.swf"
			name="fm_swfloader_254609433"
			width="650"
			height="400">
		<param name="flashvars" value="contentURL=http://www.sevenson.com.au/content/actionscript/regexp/regexp.swf&amp;previewURL=http://www.sevenson.com.au/content/actionscript/regexp/preview.jpg&amp;clickToLoad=true&amp;corners=''" />
		<param name="quality" value="high" />
		<param name="wmode" value="transparent" />
	<!--<![endif]-->
		
<div class="missingFlash">Flash should be here somewhere?</div>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>Hope that helps you out!</p>
<h2 id="regexpmethods">Regular Expression Methods</h2>
<p>Here is are the Regular Expression methods that are used in AS3</p>
<hr/>
<h4>RegExp.test(string:String):Boolean</h4>
<p>Simply tests to see if the pattern is matched anywhere in the string</p>
<ul>
<li>if &#8216;g&#8217; flag is set it searches from the .lastIndex, and also sets it after each .test()</li>
<li>if &#8216;g&#8217; is not set, then the search is always done from the start</li>
</ul>
<p><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/RegExp.html#test()" title="ActionScript Language Reference - RegExp.test()">Click here</a> for the official documentation</p>
<hr/>
<h4>RegExp.exec(string:String):Object</h4>
<p>Returns an object with with various properties relating to the pattern. The properties are</p>
<ul>
<li>[0],[1] etc &#8211; Array like access &#8211; [pattern, group1, group2]</li>
<li>index &#8211; position of the match</li>
<li>input &#8211; the string that was tested</li>
</ul>
<p>If there is no match then this will return null.</p>
<ul>
<li>if the &#8216;g&#8217; flag is set, it can be called multiple times</li>
<li>if the &#8216;g&#8217; flag is not set, it will only return the first match</li>
</ul>
<p><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/RegExp.html#exec()" title="ActionScript Language Reference - RegExp.exec()">Click here</a> for the official documentation</p>
<hr/>
<h4>String.replace(pattern:RegExp, replacement:String):Boolean</h4>
<p>Replaces the pattern in the string with the replacement string.</p>
<ul>
<li>if &#8216;g&#8217; flag is set it replaces all matches in the string</li>
<li>if &#8216;g&#8217; is not set,only the first match is replaced</li>
<li>you can use group references like $1, $2, $`, $&#038;, $&#8217;</li>
</ul>
<p><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html#replace()" title="ActionScript Language Reference - String.replace()">Click here</a> for the official documentation</p>
<hr/>
<h4>String.search(pattern:RegExp):int</h4>
<p>Returns the index of where the pattern is found in the string.</p>
<ul>
<li>returns -1 if there is no match</li>
<li>ignores the &#8216;g&#8217; flag and &#8216;.lastIndex&#8217; property</li>
</ul>
<p><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html#search()" title="ActionScript Language Reference - String.search()">Click here</a> for the official documentation</p>
<hr/>
<h4>String.match(pattern:RegExp):Array</h4>
<p>Returns an Array populated with values relating to the pattern.</p>
<ul>
<li>if the &#8216;g&#8217; flag is not set, the array will be in the form of [fullpattern, group 1, group2]</li>
<li>if the &#8216;g&#8217; flag is set, the array will contain all the possible full pattern matches &#8211; [fullpattern1, fullpattern2]</li>
</ul>
<p><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html#match()" title="ActionScript Language Reference - String.match()">Click here</a> for the official documentation</p>
<hr/>
<h2 id="syntax">Regular Expression Syntax</h2>
<p>Here is a quick run-through of the regular expression syntax as a bit of reference.</p>
<h3>Constructors</h3>
<p>There are two ways to set up a regular expression.</p>
<table class="reftable">
<tr>
<td><strong>/pattern/flags</strong></td>
<td><strong>Literal:</strong><br/>Just type the shorthand method, and bam, regular expression</td>
</tr>
<tr class="odd">
<td><strong>new RegExp(pattern:String, flags:String)</strong></td>
<td><strong>Constructor:</strong><br/>Construct it like any type of object, passing in a string of the pattern and the flags as parameters.<br/>Note: If using RegExp then you need to escape sequences &#8211; \\d instead of the normal \d</td>
</tr>
</table>
<h3>Flags</h3>
<p>Flags change the way the regular expression works. They also can effect the results of the regular expression methods in different ways.</p>
<table class="reftable">
<tr>
<td><strong>g</strong></td>
<td>Global</td>
</tr>
<tr class="odd">
<td><strong>i</strong></td>
<td>Case-insensitive</td>
</tr>
<tr>
<td><strong>m</strong></td>
<td>Multi-line</td>
</tr>
<tr class="odd">
<td><strong>s</strong></td>
<td>Dot all</td>
</tr>
<tr>
<td><strong>x</strong></td>
<td>Extended</td>
</tr>
</table>
<h3>Meta Characters</h3>
<p>Meta Characters are characters that have a special meaning when used inside a Regular Expression pattern.</p>
<table class="reftable">
<tr>
<td><strong>^</strong> (caret)</td>
<td>Matches the start of a string<br/>If using &#8216;multiline&#8217; (m) then it will match the start of a line.</td>
</tr>
<tr class="odd">
<td><strong>$</strong></td>
<td>Matches the end of a string<br/>If using multiline (m) then it will match the end of a line.</td>
</tr>
<tr>
<td><strong>\</strong></td>
<td>Escapes special characters. \d, \w, \\, etc </td>
</tr>
<tr class="odd">
<td><strong>.</strong> (period)</td>
<td>Matches any single character. <br/>If using the &#8216;dot all&#8217; (s) flag this will match new lines as well.</td>
</tr>
<tr>
<td><strong>*</strong></td>
<td>Matches the previous character 0 or more times.</td>
</tr>
<tr class="odd">
<td><strong>+</strong></td>
<td>Matches the previous character 1 or more times.</td>
</tr>
<tr>
<td><strong>?</strong></td>
<td>Matches previous character 0 or 1 time</td>
</tr>
<tr class="odd">
<td><strong>(  )</strong></td>
<td>Groups
<ul>
<li>define a scope of a quantifier (walla){1,2}</li>
<li>confine scope of the | alternator</li>
<li>used to define back references</li>
</ul>
</td>
</tr>
<tr>
<td><strong>[ ]</strong></td>
<td>Character Classes
<ul>
<li>Defines possible for matches for a single character</li>
<li>You can use hyphens to define a range &#8211; [A-Za-z0-9]</li>
<li>Backslash (\) escapes ] and -</li>
<li>^ in first position negates set</li>
<li>note &#8211; meta characters are treated as normal characters and need to be escaped to work &#8211; [\\d]</li>
</ul>
</td>
</tr>
<tr class="odd">
<td><strong>|</strong></td>
<td>Alternation / Logical Or &#8211; allows you to test between multiple patters<br/>(abc|xyz) matches abc or xyz<br/>/1|2|3|4|5/ equals [12345]</td>
</tr>
</table>
<h3>Meta Sequences</h3>
<p>Meta Sequences are special string that can be used to represent specific characters in a pattern.</p>
<table class="reftable">
<tr>
<td><strong>{n}<br/>{n,}<br/>{n,m}</strong></td>
<td>Numeric quantifier
<ul>
<li>A{27} = A 27 times</li>
<li>A{3,} = A 3 or more times</li>
<li>A{3,5} = A 3 to 5 times</li>
</ul>
</td>
</tr>
<tr class="odd">
<td><strong>\b</strong></td>
<td>Matches the position between a word character and a non-word character.  If first or last character in the string is a word character, also matches the start/end.</td>
</tr>
<tr>
<td><strong>\B</strong></td>
<td>Matches the position between 2 word characters.  Also matches the position between 2 non word characters.</td>
</tr>
<tr class="odd">
<td><strong>\d<br/>\D</strong></td>
<td>Decimals<br/>Non-decimals</td>
</tr>
<tr>
<td><strong>\f<br/>\n<br/>\r<br/>\v</strong></td>
<td>Form feed<br/>Newline<br/>Return<br/>Vertical feed</td>
</tr>
<tr class="odd">
<td><strong>\s<br/>\S</strong></td>
<td>Whitespace &#8211; space, tab, newline, return<br/>Non-whitespace</td>
</tr>
<tr>
<td><strong>\t</strong></td>
<td>Tab character</td>
</tr>
<tr class="odd">
<td><strong>\w<br/>\W</strong></td>
<td>Word character &#8211; A-Z, a-z, 0-9,_<br/>Non-word character</td>
</tr>
<tr>
<td><strong>\unnnn</strong></td>
<td>Matches unicode nnnn</td>
</tr>
<tr class="odd">
<td><strong>\xnn</strong></td>
<td>ascii character</td>
</tr>
</table>
<h3>Greedy Vs Non-Greedy</h3>
<p>Normally quantifiers like <strong>*</strong> and <strong>+</strong> are greedy, as in, they will try and match as much as they can.  They can be made lazy by adding a <strong>?</strong> afterward.</p>
<table class="reftable">
<tr>
<td>/&lt;p&gt;.*/&lt;/p&gt;/</td>
<td>greedy &#8211; the * will match as many characters as possible</td>
</tr>
<tr class="odd">
<td>/&lt;p&gt;.*?/&lt;/p&gt;/</td>
<td>lazy &#8211; the * will match the least amount of characters</td>
</tr>
</table>
<h3>Capturing sub-strings</h3>
<p>Sometimes when you are looking for a pattern in a string you need to work with a sub-string.  Maybe it is to replace it, or reference it is a new string.  Maybe you don&#8217;t know what exactly it is you have to match. That&#8217;s where this stuff comes in.</p>
<table class="reftable">
<tr>
<td><strong>\1, \2, etc</strong></td>
<td>Back reference<br/>Matches an earlier defined group in the pattern<br/>eg: /(\d+)-by-<strong>\1</strong>/ matches 48-by-48</td>
</tr>
<tr class="odd">
<td><strong>$1 &#8211; $nn</strong></td>
<td>Group references<br/>Can be used to reference groups in the String.replace() and RegExp.exec() methods<br />
<code class="as"><br />
	var str:String = "Hi, Sev";<br />
	str = str.replace(/Hi, (w+)/,"$1, hello");<br />
	trace(str);		// Sev, hello<br />
</code>
		</td>
</tr>
<tr>
<td><strong>$$</strong></td>
<td>Puts a $ in the replacement text (not really a sub-string reference, but sorta matches up with this stuff.)</td>
</tr>
<tr class="odd">
<td><strong>$&#038;</strong></td>
<td>References the fully matched string</td>
</tr>
<tr>
<td><strong>$`</strong></td>
<td>References the string preceding the matched pattern</td>
</tr>
<tr class="odd">
<td><strong>$&#8217;</strong></td>
<td>References the string following the matched pattern</td>
</tr>
<tr>
<td><strong>?:</strong></td>
<td>Non-capturing Group<br/>Sometimes you don&#8217;t want a group to be captured for back references, etc.  This is can be done by adding <strong>?:</strong> at the start of the group.<br/>eg: /(?:group)/</td>
</tr>
<tr class="odd">
<td><strong>(?P&lt;name&gt;.*):</strong></td>
<td>Named groups<br/>When used in RegExp.exec(), the group will be stored under a property with the &#8216;name&#8217;.<br/>Note: this is an AS3 only Regular Expression syntax.</td>
</tr>
</table>
<h2>Links</h2>
<p>Here are some links to some sites that help with regular expressions.</p>
<ul>
<li><a href="http://www.regular-expressions.info/" title="Regular Expressions Info">http://www.regular-expressions.info/</a></li>
<li><a href="http://regexlib.com/" title="Regular Expressions Library">http://regexlib.com/</a></li>
<li><a href="http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&#038;file=00000112.html" title="Flash LiveDocs Reference">Flash LiveDocs Reference on Regular Expression Syntax</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.sevenson.com.au/actionscript/testing-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
