<?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>Alastair&#039;s Axioms &#187; RegEx</title>
	<atom:link href="http://blog.alastairdawson.com/category/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.alastairdawson.com</link>
	<description>Flex, Ruby, etc. etc.</description>
	<lastBuildDate>Thu, 11 Feb 2010 19:28:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Regular expressions make my head hurt</title>
		<link>http://blog.alastairdawson.com/2008/01/22/regular-expressions-make-my-head-hurt/</link>
		<comments>http://blog.alastairdawson.com/2008/01/22/regular-expressions-make-my-head-hurt/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 20:58:57 +0000</pubDate>
		<dc:creator>Alastair</dc:creator>
				<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.vixiom.com/2008/01/22/regular-expressions-make-my-head-hurt/</guid>
		<description><![CDATA[A site to dull the pain Rubular. Via Ruby Inside.
]]></description>
			<content:encoded><![CDATA[<p>A site to dull the pain <a href="http://www.rubular.com/">Rubular</a>. Via <a href="http://www.rubyinside.com/interesting-ruby-tidbits-that-dont-need-separate-posts-14-699.html">Ruby Inside</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alastairdawson.com/2008/01/22/regular-expressions-make-my-head-hurt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mask Credit Card Numbers with Regular Expressions in Rails</title>
		<link>http://blog.alastairdawson.com/2006/09/14/mask-credit-card-numbers-with-regular-expressions-in-rails/</link>
		<comments>http://blog.alastairdawson.com/2006/09/14/mask-credit-card-numbers-with-regular-expressions-in-rails/#comments</comments>
		<pubDate>Thu, 14 Sep 2006 17:03:39 +0000</pubDate>
		<dc:creator>Alastair</dc:creator>
				<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.vixiom.com/2006/09/14/mask-credit-card-numbers-with-regular-expressions-in-rails/</guid>
		<description><![CDATA[Sometimes you need to display sensitive information in a browser, such as the credit card a customer has on file. Obviously you don&#8217;t want to show the entire card number in case the customer leaves there browser open on a public computer, or even worse someone hacks into their account. However, you do need to [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to display sensitive information in a browser, such as the credit card a customer has on file. Obviously you don&#8217;t want to show the entire card number in case the customer leaves there browser open on a public computer, or even worse someone hacks into their account. However, you do need to show a piece of the information otherwise the customer would have no idea which credit card was on file. How to mask the credit card number? Regular Expressions to the rescue!</p>
<p>When I first searched for a way to do this I was surprised that I couldn&#8217;t find any examples, there&#8217;s a ton of regex tutorials for checking if emails are valid but none for masking credit card numbers. Here&#8217;s my solution in Rails.</p>
<p>Let&#8217;s say the customer&#8217;s card number is 5555-4444-3333-2222 (@customer.card_number = 5555-4444-3333-2222). First strip everything but the numbers.</p>
<pre class="textmate-source"><span class="source source_ruby source_ruby_rails"><span class="meta meta_rails meta_rails_controller">    card_masked ||= <span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby">@customer</span>.card_number.gsub(<span class="string string_regexp string_regexp_classic string_regexp_classic_ruby">/<span class="string string_regexp string_regexp_character-class string_regexp_character-class_ruby">[^0-9]</span>/</span>, <span class="string string_quoted string_quoted_single string_quoted_single_ruby">''</span>)</span></span></pre>
<p>Then mask all but the last four digits. </p>
<pre class="textmate-source"><span class="source source_ruby source_ruby_rails"><span class="meta meta_rails meta_rails_controller">    <span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby">@card_masked</span> = card_masked.sub(<span class="string string_regexp string_regexp_classic string_regexp_classic_ruby">/^<span class="string string_regexp string_regexp_group string_regexp_group_ruby">(<span class="string string_regexp string_regexp_character-class string_regexp_character-class_ruby">[0-9]</span>+)(<span class="string string_regexp string_regexp_character-class string_regexp_character-class_ruby">[0-9]</span><span class="string string_regexp string_regexp_arbitrary-repitition string_regexp_arbitrary-repitition_ruby">{4}</span>)</span>$/</span>) {<span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block"> </span><span class="string string_quoted string_quoted_single string_quoted_single_ruby">'*'</span> * <span class="variable variable_other variable_other_readwrite variable_other_readwrite_global variable_other_readwrite_global_pre-defined variable_other_readwrite_global_pre-defined_ruby">$1</span>.length + <span class="variable variable_other variable_other_readwrite variable_other_readwrite_global variable_other_readwrite_global_pre-defined variable_other_readwrite_global_pre-defined_ruby">$2</span> }</span></span></pre>
<p>That&#8217;s it! @card_masked will out put as ************2222 </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alastairdawson.com/2006/09/14/mask-credit-card-numbers-with-regular-expressions-in-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails validation make sure your user passwords are strong</title>
		<link>http://blog.alastairdawson.com/2006/08/12/rails-validation-make-sure-your-user-passwords-are-strong/</link>
		<comments>http://blog.alastairdawson.com/2006/08/12/rails-validation-make-sure-your-user-passwords-are-strong/#comments</comments>
		<pubDate>Sat, 12 Aug 2006 17:04:33 +0000</pubDate>
		<dc:creator>Alastair</dc:creator>
				<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.vixiom.com/2006/08/12/rails-validation-make-sure-your-user-passwords-are-strong/</guid>
		<description><![CDATA[Most user created passwords are astoundingly weak (&#8217;12345&#8242;, &#8216;mypass&#8217;). How do you make them stronger? Don&#8217;t give them a choice!
Here&#8217;s how to validate a password in RoR to make sure it&#8217;s strong using a regular expression (regex).
In your model add a custom validate method (after the regular validation) that adds an error unless the password [...]]]></description>
			<content:encoded><![CDATA[<p>Most user created passwords are astoundingly weak (&#8217;12345&#8242;, &#8216;mypass&#8217;). How do you make them stronger? <em>Don&#8217;t give them a choice!</em></p>
<p>Here&#8217;s how to validate a password in RoR to make sure it&#8217;s strong using a regular expression (regex).<br />
In your model add a custom validate method (after the regular validation) that adds an error unless the password is valid.</p>
<p>The &#8216;password_validate?&#8217; method</p>
<pre class="textmate-source"><span class="source source_ruby source_ruby_rails"><span class="meta meta_rails meta_rails_model"><span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">password_valid?</span></span> <span class="constant constant_language constant_language_pseudo-variable constant_language_pseudo-variable_ruby">self</span>.password =~ <span class="string string_regexp string_regexp_classic string_regexp_classic_ruby">/^<span class="string string_regexp string_regexp_group string_regexp_group_ruby">(?=.*<span class="constant constant_character constant_character_escape constant_character_escape_ruby">d</span>)(?=.*<span class="string string_regexp string_regexp_character-class string_regexp_character-class_ruby">[a-z]</span>)(?=.*<span class="string string_regexp string_regexp_character-class string_regexp_character-class_ruby">[A-Z]</span>)(?!.*<span class="constant constant_character constant_character_escape constant_character_escape_ruby">s</span>)</span>.<span class="string string_regexp string_regexp_arbitrary-repitition string_regexp_arbitrary-repitition_ruby">{8,15}</span>$/</span> <span class="keyword keyword_control keyword_control_ruby">end</span></span></span></pre>
<p>In this case the regular expression /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,15}$/ is checking that the password is 8-15 characters long &#8216;.{8,15}&#8217;, and it contains at least one uppercase letter &#8216;(?=.*[A-Z])&#8217; and one digit &#8216;(?=.*\d)&#8217;. Actually it also checks for at least one lowercase letter as well &#8216;(?=.*[a-z])&#8217; but most users usually include that, it also checks that there&#8217;s no funky characters  &#8216;(?!.*\s)&#8217;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alastairdawson.com/2006/08/12/rails-validation-make-sure-your-user-passwords-are-strong/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
