<?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>In Flagrante Delicto! &#187; RegExp</title>
	<atom:link href="http://inflagrantedelicto.memoryspiral.com/tag/regexp/feed/" rel="self" type="application/rss+xml" />
	<link>http://inflagrantedelicto.memoryspiral.com</link>
	<description>Joseph Labrecque: Senior Interactive Software Engineer - Artist - Author - Speaker - Adobe Education Leader - Adobe Community Professional</description>
	<lastBuildDate>Mon, 06 Feb 2012 15:10:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Parsing Tweets with the TwitterString Class</title>
		<link>http://inflagrantedelicto.memoryspiral.com/2010/01/parsing-tweets-with-the-twitterstring-class/</link>
		<comments>http://inflagrantedelicto.memoryspiral.com/2010/01/parsing-tweets-with-the-twitterstring-class/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 19:13:59 +0000</pubDate>
		<dc:creator>Joseph Labrecque</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[RegExp]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://inflagrantedelicto.memoryspiral.com/?p=1387</guid>
		<description><![CDATA[While building a little Twitter aggregator for an upcoming conference, I found myself in need of a set of methods to create hyperlinks from three distinct elements that can be included within a tweet; links, usernames, and hashtags. I was &#8230; <a href="http://inflagrantedelicto.memoryspiral.com/2010/01/parsing-tweets-with-the-twitterstring-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While building a little Twitter aggregator for an upcoming conference, I found myself in need of a set of methods to create hyperlinks from three distinct elements that can be included within a tweet; links, usernames, and hashtags.</p>
<p>I was able to find regular expressions to do all the heavy lifting from various sources on the Web and have created a class which pretty much does all the processing with one method call.  Here&#8217;s an example of the thing working, followed by example code, and the class itself.</p>
<h2>Example SWF</h2>
<p>
<object width="300" height="100">
<param name="movie" value="http://inflagrantedelicto.memoryspiral.com/wp-content/uploads/2010/01/TwitterStringDemo.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#000000"></param>
<param name="allowScriptAccess" value="always"></param>
<param name="allowFullScreen" value="true"></param>
<embed type="application/x-shockwave-flash" width="300" height="100" src="http://inflagrantedelicto.memoryspiral.com/wp-content/uploads/2010/01/TwitterStringDemo.swf" quality="high" bgcolor="#000000" wmode="window" menu="false" allowFullScreen="true" ></embed>
</object>
</p>
<hr/>
<h2>Example MXML</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Application</span> <span style="color: #000066;">xmlns:fx</span>=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #009900;">			   <span style="color: #000066;">xmlns:s</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span> </span>
<span style="color: #009900;">			   <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span> </span>
<span style="color: #009900;">			   <span style="color: #000066;">creationComplete</span>=<span style="color: #ff0000;">&quot;init()&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> </span>
<span style="color: #009900;">			   <span style="color: #000066;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#1E1E1E&quot;</span> <span style="color: #000066;">preloaderBaseColor</span>=<span style="color: #ff0000;">&quot;#989898&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">			import flashx.textLayout.elements.Configuration;</span>
<span style="color: #339933;">			import flashx.textLayout.elements.TextFlow;</span>
<span style="color: #339933;">			import flashx.textLayout.conversion.TextConverter;</span>
<span style="color: #339933;">			import flashx.textLayout.formats.TextLayoutFormat;</span>
<span style="color: #339933;">			import com.fracturedvisionmedia.utils.TwitterString;</span>
&nbsp;
<span style="color: #339933;">			private var myTxt:String = &quot;Everyone should follow @josephlabrecque (http://bit.ly/7NkqrB) - really cool stuff and super-informative insights! #Awesome #Super #LOL&quot;;</span>
&nbsp;
<span style="color: #339933;">			private function init():void {</span>
<span style="color: #339933;">				// Configure styling the TextFlow links to match richTxt</span>
<span style="color: #339933;">				var cfg:Configuration = TextFlow.defaultConfiguration;</span>
<span style="color: #339933;">				var normalTLF:TextLayoutFormat = new TextLayoutFormat(cfg.defaultLinkNormalFormat);</span>
<span style="color: #339933;">				normalTLF.color = 0xDCD9D9;</span>
<span style="color: #339933;">				cfg.defaultLinkNormalFormat = normalTLF;</span>
<span style="color: #339933;">				TextFlow.defaultConfiguration = cfg;</span>
&nbsp;
<span style="color: #339933;">				// Import tweet as HTML</span>
<span style="color: #339933;">				richTxt.textFlow = TextConverter.importToFlow(TwitterString.instance.parseTweet(myTxt), TextConverter.TEXT_FIELD_HTML_FORMAT);</span>
<span style="color: #339933;">			}</span>
<span style="color: #339933;">		]]&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:RichEditableText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;richTxt&quot;</span> <span style="color: #000066;">selectable</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">editable</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">right</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">top</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">bottom</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">color</span>=<span style="color: #ff0000;">&quot;#DCD9D9&quot;</span> <span style="color: #000066;">left</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h2>TwitterString Class</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">fracturedvisionmedia</span>.<span style="color: #006600;">utils</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * The TwitterString class assists with the parsing of a tweet to add hyperlinks 
	 * around Links, HashTags, and UserNames in a tweet.
	 * @author Joseph Labrecque
	 * v. 0.1.2
	 */</span> 
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> TwitterString <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> _instance:TwitterString = <span style="color: #000000; font-weight: bold;">new</span> TwitterString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TwitterString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_instance <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;TwitterString can only be accessed through TwitterString.instance&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:TwitterString <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> _instance;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> parseTweet<span style="color: #66cc66;">&#40;</span>t:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> step1:<span style="color: #0066CC;">String</span> = parseHyperlinks<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> step2:<span style="color: #0066CC;">String</span> = parseUsernames<span style="color: #66cc66;">&#40;</span>step1<span style="color: #66cc66;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">var</span> step3:<span style="color: #0066CC;">String</span> = parseHashtags<span style="color: #66cc66;">&#40;</span>step2<span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">return</span> step3;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> parseUsernames<span style="color: #66cc66;">&#40;</span>t:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> result:<span style="color: #0066CC;">String</span> = t.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span>^<span style="color: #66cc66;">|</span>\s<span style="color: #66cc66;">&#41;</span><span style="color: #808080; font-style: italic;">#(\w+)/g, &quot;$1#&lt;a href='http://search.twitter.com/search?q=$2' target='_blank'&gt;$2&lt;/a&gt;&quot;);</span>
			<span style="color: #b1b100;">return</span> result;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> parseHashtags<span style="color: #66cc66;">&#40;</span>t:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> result:<span style="color: #0066CC;">String</span> = t.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span>^<span style="color: #66cc66;">|</span>\s<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">@</span><span style="color: #66cc66;">&#40;</span>\w+<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span>g, <span style="color: #ff0000;">&quot;$1@&lt;a href='http://www.twitter.com/$2' target='_blank'&gt;$2&lt;/a&gt;&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> result;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> parseHyperlinks<span style="color: #66cc66;">&#40;</span>t:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> urlPattern:RegExp = <span style="color: #000000; font-weight: bold;">new</span> RegExp<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;(((f|ht){1}tp://)[-a-zA-Z0-9@:%_<span style="color: #000099; font-weight: bold;">\+</span>.~#?&amp;//=]+)&quot;</span>, <span style="color: #ff0000;">&quot;g&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">var</span> result:<span style="color: #0066CC;">String</span> = t.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span>urlPattern, <span style="color: #ff0000;">&quot;&lt;a href='$1' target='_blank'&gt;$1&lt;/a&gt;&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> result;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><a href='http://inflagrantedelicto.memoryspiral.com/wp-content/uploads/2010/02/TwitterString.as'> Download TwitterString</a></p>
]]></content:encoded>
			<wfw:commentRss>http://inflagrantedelicto.memoryspiral.com/2010/01/parsing-tweets-with-the-twitterstring-class/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

