<?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>Sean&#039;s Blog &#187; regex</title>
	<atom:link href="http://seanbehan.com/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 18 Jan 2012 21:44: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>Email Regex</title>
		<link>http://seanbehan.com/tips-snippets/email-regex/</link>
		<comments>http://seanbehan.com/tips-snippets/email-regex/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 03:38:52 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[tips & snippets]]></category>
		<category><![CDATA[email parsing]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1445</guid>
		<description><![CDATA[Regular Expression that Matches Email Addresses: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Ftips-snippets%2Femail-regex%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Email Regex" data-url="http://seanbehan.com/tips-snippets/email-regex/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>Regular Expression that Matches Email Addresses:</p>
<pre class="wp-code-highlight prettyprint">
/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/tips-snippets/email-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Absolutize Relative Links Using PHP and Preg_Replace_Callback</title>
		<link>http://seanbehan.com/php/absolutize-relative-links-using-php-and-preg_replace_callback/</link>
		<comments>http://seanbehan.com/php/absolutize-relative-links-using-php-and-preg_replace_callback/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 17:29:41 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[absolute links]]></category>
		<category><![CDATA[file_get_contents]]></category>
		<category><![CDATA[preg_replace_callback]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[relative links]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=727</guid>
		<description><![CDATA[I was in the market for a simple php script to replace hrefs with their absolute paths from scraped web pages. I wrote one myself. I used the preg_replace_callback function so that I could pass the parsed results as a single variable. &#38;lt;?php $domain = &#34;http://seanbehan.com&#34;; $pattern = &#34;/\bhref=[\&#34;&#124;'](.*?)[\&#34;&#124;']/&#34;; $string = file_get_contents($domain); // prepends relative [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fphp%2Fabsolutize-relative-links-using-php-and-preg_replace_callback%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Absolutize Relative Links Using PHP and Preg_Replace_Callback" data-url="http://seanbehan.com/php/absolutize-relative-links-using-php-and-preg_replace_callback/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>I was in the market for a simple php script to replace hrefs with their absolute paths from scraped web pages. I  wrote one myself. I used the preg_replace_callback function so that I could pass the parsed results as a single variable.</p>
<pre class="wp-code-highlight prettyprint">
&amp;lt;?php
$domain = &quot;http://seanbehan.com&quot;;
$pattern = &quot;/\bhref=[\&quot;|'](.*?)[\&quot;|']/&quot;;
$string = file_get_contents($domain);

// prepends relative links w/ $domain skips returns the match if already absolute
function replace_href($match){
  global $domain;
  if(substr($match[1], 0, 7)!==&quot;http://&quot; &amp;&amp; substr($match[1],0,8)!==&quot;https://&quot;){
    return &quot;href='&quot;.$domain.$match[1].&quot;'&quot;;
  } else {
    return &quot;href='&quot;.$match[1].&quot;'s&quot;;
  }
}
print preg_replace_callback($pattern, &quot;replace_href&quot;, $string);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/php/absolutize-relative-links-using-php-and-preg_replace_callback/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Regular Expression for finding absolute URLs</title>
		<link>http://seanbehan.com/programming/regular-expression-for-finding-absolute-urls/</link>
		<comments>http://seanbehan.com/programming/regular-expression-for-finding-absolute-urls/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 03:27:02 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=531</guid>
		<description><![CDATA[Regular Expression for finding absolute URLs in a bunch of text&#8230; like a log file. /(http:(.*?)\s)/]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fprogramming%2Fregular-expression-for-finding-absolute-urls%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Regular Expression for finding absolute URLs" data-url="http://seanbehan.com/programming/regular-expression-for-finding-absolute-urls/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>Regular Expression for finding absolute URLs in a bunch of text&#8230; like a log file.</p>
<pre class="wp-code-highlight prettyprint">
/(http:(.*?)\s)/
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/programming/regular-expression-for-finding-absolute-urls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Email Obfuscation and Extraction from Text with Rails</title>
		<link>http://seanbehan.com/ruby-on-rails/email-obfuscation-and-extraction-from-text-with-rails/</link>
		<comments>http://seanbehan.com/ruby-on-rails/email-obfuscation-and-extraction-from-text-with-rails/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 16:50:29 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[hyperlinking]]></category>
		<category><![CDATA[obfuscation]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[recipes]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=423</guid>
		<description><![CDATA[There is a helper method for handling the obfuscation of email addresses in Rails. mail_to &#34;me@domain.com&#34;, &#34;My email&#34;, :encode =&#62; &#34;hex&#34; # =&#62; &#60;a href=&#34;mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d&#34;&#62;My email&#60;/a&#62; If you want to then extract an email address(or all email addresses) from a block of text here is the code. I created a helper function called &#8220;emailitize&#8221; and [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Femail-obfuscation-and-extraction-from-text-with-rails%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Email Obfuscation and Extraction from Text with Rails" data-url="http://seanbehan.com/ruby-on-rails/email-obfuscation-and-extraction-from-text-with-rails/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>There is a helper method for handling the obfuscation of email addresses in Rails.</p>
<pre class="wp-code-highlight prettyprint">
mail_to &quot;me@domain.com&quot;, &quot;My email&quot;, :encode =&gt; &quot;hex&quot;
 # =&gt; &lt;a href=&quot;mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d&quot;&gt;My email&lt;/a&gt;
</pre>
<p>If you want to then extract an email address(or all email addresses) from a block of text here is the code. I created a helper function called &#8220;emailitize&#8221; and put it in the ApplicationHelper module inside helpers/application_helper.rb</p>
<pre class="wp-code-highlight prettyprint">
module ApplicationHelper
  #takes a string and will return the same string but with email addresses encoded and hyperlinked
  def emailitize(text)
    text.gsub(/([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})/i) {|m|
        mail_to(m, m.gsub(&quot;@&quot;, &quot;&lt;small&gt;[at]&lt;/small&gt;&quot;), :encode=&gt;:hex)
    }
  end
end
</pre>
<p>It&#8217;s important to remember that you&#8217;ll need to pass a block to the gsub method. You can&#8217;t do something like this instead</p>
<pre class="wp-code-highlight prettyprint">
text.gsub( /([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})/i, mail_to('\\1@\\2', '\\1@\\2', :encode=&gt;:hex) )
</pre>
<p>It will work except the encode will fail. It will evaluate the &#8216;\\1@\\2&#8242; strings rather than as dynamic variables.</p>
<p>You can then use this function in your views</p>
<pre class="wp-code-highlight prettyprint">
&lt;%= emailitize @job.how_to_apply %&gt;
</pre>
<p>More information is available in the Rails and Ruby docs:</p>
<p>http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001887</p>
<p>http://ruby-doc.org/core/classes/String.html#M000817</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/email-obfuscation-and-extraction-from-text-with-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Parse for Links with Prototype JS</title>
		<link>http://seanbehan.com/programming/parse-for-links-with-prototype-js/</link>
		<comments>http://seanbehan.com/programming/parse-for-links-with-prototype-js/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 15:23:06 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://bseanvt.wordpress.com/?p=79</guid>
		<description><![CDATA[Parsing for links with the Prototype javascript library is easy. Here is the pattern for finding links /(http&#124;https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^ =%&#38;amp;amp;:/~\+#]*[\w\-\@?^=%&#38;amp;amp;/~\+#])?/ And to implement it you can loop through your containers that might contain links document.observe(&#34;dom:loaded&#34;, function(){ var posts = $$(&#34;div#posts&#34;); for(var i = 0; i &#38;lt; posts.length; i++){ var link_regex = /(http&#124;https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^ =%&#38;amp;amp;:/~\+#]*[\w\-\@?^=%&#38;amp;amp;/~\+#])?/; var parsed_string = [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fprogramming%2Fparse-for-links-with-prototype-js%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Parse for Links with Prototype JS" data-url="http://seanbehan.com/programming/parse-for-links-with-prototype-js/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>Parsing for links with the Prototype javascript library is easy. Here is the pattern for finding links</p>
<pre class="wp-code-highlight prettyprint">
/(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^
=%&amp;amp;amp;:/~\+#]*[\w\-\@?^=%&amp;amp;amp;/~\+#])?/
</pre>
<p>And to implement it you can loop through your containers that might contain links</p>
<pre class="wp-code-highlight prettyprint">
document.observe(&quot;dom:loaded&quot;, function(){
var posts = $$(&quot;div#posts&quot;);
for(var i = 0; i &amp;lt; posts.length; i++){
var link_regex = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^
=%&amp;amp;amp;:/~\+#]*[\w\-\@?^=%&amp;amp;amp;/~\+#])?/;
var parsed_string = posts[i].innerHTML.gsub(link_regex, '&amp;lt;a href=&quot;#{0}&quot;
target=&quot;_blank&quot;&amp;gt;#{0}&amp;lt;/a&amp;gt;');
posts[i].innerHTML = parsed_string;
}
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/programming/parse-for-links-with-prototype-js/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

