<?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>Chris Irish &#124; Ruby, Rails, Javascript Developer &#124; Phoenix, AZ &#187; Regular Expressions</title>
	<atom:link href="http://www.christopherirish.com/tag/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christopherirish.com</link>
	<description>Ruby, Rails, Javascript, all things web development related</description>
	<lastBuildDate>Mon, 19 Jan 2026 07:43:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Splitting Rails DOM ids with RegEx Look Aheads</title>
		<link>http://www.christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads/</link>
		<comments>http://www.christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 08:27:02 +0000</pubDate>
		<dc:creator>Irish</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.christopherirish.com/?p=530</guid>
		<description><![CDATA[In my Rails erb views I make good use of the dom_id view helper method. It basically introspects the object you pass to it and creates a unique id to use in your markup, based on the object type and database id. So for example if I had a Post with id 1, I could [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In my Rails erb views I make good use of the dom_id view helper method.  It basically introspects the object you pass to it and creates a unique id to use in your markup, based on the object type and database id.  So for example if I had a Post with id 1, I could do:</p>
<pre class="textmate-source sunburst"><span class='linenum'>    1</span> <span class="text text_html text_html_ruby">
<span class='linenum'>    2</span> <span class="meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html"><span class="punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html">&lt;</span><span class="entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html">li</span> <span class="meta meta_attribute-with-value meta_attribute-with-value_id meta_attribute-with-value_id_html"><span class="entity entity_other entity_other_attribute-name entity_other_attribute-name_id entity_other_attribute-name_id_html">id</span><span class="punctuation punctuation_separator punctuation_separator_key-value punctuation_separator_key-value_html">=</span><span class="string string_quoted string_quoted_double string_quoted_double_html"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_html">"</span><span class="meta meta_toc-list meta_toc-list_id meta_toc-list_id_html"><span class="source source_ruby source_ruby_embedded source_ruby_embedded_html"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">&lt;%=</span> dom_id<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>post</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span> <span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">%&gt;</span></span></span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_html">"</span></span></span><span class="punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html">&gt;</span></span>
</span><span class='linenum'>    3</span> </pre>
<p>And it would be rendered as</p>
<pre class="textmate-source sunburst"><span class='linenum'>    1</span> <span class="text text_html text_html_ruby">
<span class='linenum'>    2</span> <span class="meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html"><span class="punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html">&lt;</span><span class="entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html">li</span> <span class="meta meta_attribute-with-value meta_attribute-with-value_id meta_attribute-with-value_id_html"><span class="entity entity_other entity_other_attribute-name entity_other_attribute-name_id entity_other_attribute-name_id_html">id</span><span class="punctuation punctuation_separator punctuation_separator_key-value punctuation_separator_key-value_html">=</span><span class="string string_quoted string_quoted_double string_quoted_double_html"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_html">"</span><span class="meta meta_toc-list meta_toc-list_id meta_toc-list_id_html">post_1</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_html">"</span></span></span><span class="punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html">&gt;</span></span>
</span><span class='linenum'>    3</span> </pre>
<p>This is fine until you use dom_id on an object with a compound name like EmailNotification. Now dom_id will create a unique id like</p>
<p>email_notification_1</p>
<p>Which is still good until I&#8217;m doing some front-end testing and I&#8217;m parsing these dom ids to find their related db records.  Now I need to split &#8220;email_notification_1&#8243; in a way that easily separates the object class from the identifier.  How would you do that? </p>
<p>Well, what we can do here is use an advanced Regular Expression feature called a negative look ahead.  We want to say, &#8220;split the string on the last underscore&#8221;.</p>
<p>A negative look ahead uses the ?! syntax in parenthesis, everything that follows ?! in the parenthesis will be used in the negative look ahead matching.  So in our case we want to say, there should be a underscore that can be followed by any character except another underscore.  The pattern would like like this:</p>
<pre>
/_(?!.*_)/
</pre>
<p>We can confirm in irb</p>

<div class="wp-terminal">$ ree-1.8.7-head > x = "email_notification_1234"<br/>=> "email_notification_1234"<br/>ree-1.8.7-head > klass, id = x.split(/_(?!.*_)/)<br/>=> ["email_notification", "1234"] <br/></div>

<p>Likewise, If we wanted to split the dom_id on the first underscore we could just use a regular look ahead.  It is similar but uses the ?= syntax.</p>

<div class="wp-terminal">$ ree-1.8.7-head > x.split(/_(?=.*_)/)<br/>=> ["email", "notification_1234"]<br/></div>

]]></content:encoded>
			<wfw:commentRss>http://www.christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Ruby %r{} expression</title>
		<link>http://www.christopherirish.com/2010/08/12/the-ruby-r-expression/</link>
		<comments>http://www.christopherirish.com/2010/08/12/the-ruby-r-expression/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 08:13:27 +0000</pubDate>
		<dc:creator>Irish</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.christopherirish.com/?p=430</guid>
		<description><![CDATA[Tonight I was reading over some Ruby code in a gem I&#8217;m currently using and came across this line: 1 string = mode.to_s 2 string.gsub!(%r{(^.)&#124;(_.)}) { &#124;m&#124; m[m.length-1,1].upcase } 3 Two things struck me about that second line. For one I&#8217;ve never used gsub and passed it a block, thats cool. And two, what is [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Tonight I was reading over some Ruby code in a gem I&#8217;m currently using and came across this line:</p>
<pre class="textmate-source sunburst"><span class='linenum'>    1</span> <span class="source source_ruby">string <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> mode<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>to_s
<span class='linenum'>    2</span> string<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>gsub!<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_regexp string_regexp_mod-r string_regexp_mod-r_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">%r{</span><span class="string string_regexp string_regexp_group string_regexp_group_ruby"><span class="punctuation punctuation_definition punctuation_definition_group punctuation_definition_group_ruby">(</span>^.<span class="punctuation punctuation_definition punctuation_definition_group punctuation_definition_group_ruby">)</span></span>|<span class="string string_regexp string_regexp_group string_regexp_group_ruby"><span class="punctuation punctuation_definition punctuation_definition_group punctuation_definition_group_ruby">(</span>_.<span class="punctuation punctuation_definition punctuation_definition_group punctuation_definition_group_ruby">)</span></span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">}</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block"> </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">m</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span> m<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span>m<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>length<span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">-</span><span class="constant constant_numeric constant_numeric_ruby">1</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span><span class="constant constant_numeric constant_numeric_ruby">1</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>upcase <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span>
</span><span class='linenum'>    3</span> </pre>
<p>Two things struck me about that second line.  For one I&#8217;ve never used gsub and passed it a block, thats cool.  And two, what is %r{} ? I&#8217;ve never seen that before.  It turns out that in Ruby, the /&#8230;/ or %r&#8230; literals, and using the Regexp.new constructor are all the same thing.  All ways of created a regular expression.  I still think I prefer the look of the simple slashes /&#8230;/ though.</p>
<div id="hire" class="text-center">
  <a href="http://velocitylabs.io"><img src="http://www.christopherirish.com/wp-content/uploads/2018/07/velocity-labs-logo-text.png" alt="" title="velocity-labs-logo-text" width="300" class="alignleft size-full wp-image-971" /></a></p>
<p>
    Need web application development, maintenance for your existing app, or a third party code review?
  </p>
<p>
    <strong class="text-uppercase">Velocity Labs can help.</strong>
  </p>
<p>  <a href="http://velocitylabs.io#contact" class="btn btn-warning btn-block" target="_blank">Click here to get started!</a>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.christopherirish.com/2010/08/12/the-ruby-r-expression/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
