<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Groovy: Don&#8217;t Fear the RegExp</title>
	<atom:link href="http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<pubDate>Tue, 06 Jan 2009 03:08:11 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-696</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Thu, 19 Jun 2008 20:27:03 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-696</guid>
		<description>@Mibmib: I think the example you give doesn't work because the for loop is using the iterator (just like the ones I have above, like collect, inject, etc).  If you want access to the groups, you just have to use .each:

&lt;pre lang="groovy"&gt;
matcher = ( "test1 test2" =~ /test([0-9]+)/ )
matcher.each { fullMatch, number -&gt;
    println "full match: $fullMatch, number: $number"
}
&lt;/pre&gt;

prints:

full match: test1, number: 1
full match: test2, number: 2</description>
		<content:encoded><![CDATA[<p>@Mibmib: I think the example you give doesn&#8217;t work because the for loop is using the iterator (just like the ones I have above, like collect, inject, etc).  If you want access to the groups, you just have to use .each:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">matcher <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test1 test2&quot;</span> <span style="color: #66cc66;">=</span>~ /test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #cc66cc;">-9</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #66cc66;">&#41;</span>/ <span style="color: #66cc66;">&#41;</span>
matcher.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> fullMatch, number -<span style="color: #66cc66;">&gt;</span>
    <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;full match: $fullMatch, number: $number&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>prints:</p>
<p>full match: test1, number: 1<br />
full match: test2, number: 2</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mibmib</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-695</link>
		<dc:creator>Mibmib</dc:creator>
		<pubDate>Thu, 19 Jun 2008 15:23:22 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-695</guid>
		<description>Great article, thx !

I was struggling to get groups out of a find until I read this :

"A limitation of the iterator-based methods is that they don’t give you access to the individual groups"

why doesn't this work (why Is x a sting instead of list )  :

matcher = ( "test1 test2" =~ /test([0-9]+)/ )

for ( x in matcher ){
    println x
}</description>
		<content:encoded><![CDATA[<p>Great article, thx !</p>
<p>I was struggling to get groups out of a find until I read this :</p>
<p>&#8220;A limitation of the iterator-based methods is that they don’t give you access to the individual groups&#8221;</p>
<p>why doesn&#8217;t this work (why Is x a sting instead of list )  :</p>
<p>matcher = ( &#8220;test1 test2&#8243; =~ /test([0-9]+)/ )</p>
<p>for ( x in matcher ){<br />
    println x<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ted Naleid &#187; Syntactic Sugar in Groovy and Ruby</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-409</link>
		<dc:creator>Ted Naleid &#187; Syntactic Sugar in Groovy and Ruby</dc:creator>
		<pubDate>Wed, 28 May 2008 04:25:29 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-409</guid>
		<description>[...] used to think that the regular expression support in Groovy was lacking compared to Ruby, but after a little digging, I found that it&#8217;s just about as easy and powerful as Ruby [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] used to think that the regular expression support in Groovy was lacking compared to Ruby, but after a little digging, I found that it&#8217;s just about as easy and powerful as Ruby [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grails Podcast Episode 57: Newscast for May 24th 2008 &#171; Sven Haiges&#8217; Personal Blog</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-398</link>
		<dc:creator>Grails Podcast Episode 57: Newscast for May 24th 2008 &#171; Sven Haiges&#8217; Personal Blog</dc:creator>
		<pubDate>Tue, 27 May 2008 05:21:07 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-398</guid>
		<description>[...] Don&#8217;t fear the RegExp - new features in Groovy regular expressions. [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] Don&#8217;t fear the RegExp - new features in Groovy regular expressions. [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Klein</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-263</link>
		<dc:creator>Dave Klein</dc:creator>
		<pubDate>Wed, 21 May 2008 13:15:05 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-263</guid>
		<description>After seeing your enhancement to my blog post, I decided to look into RegEx more (it always just looked like comic strip swearing to me).  So thanks for giving me a starting point!  

On the down side, I can't get that song out of my head and it's getting depressing :)</description>
		<content:encoded><![CDATA[<p>After seeing your enhancement to my blog post, I decided to look into RegEx more (it always just looked like comic strip swearing to me).  So thanks for giving me a starting point!  </p>
<p>On the down side, I can&#8217;t get that song out of my head and it&#8217;s getting depressing :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-218</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Tue, 20 May 2008 15:50:09 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-218</guid>
		<description>Thanks guys I'm glad you found the post useful!

@kodeninja that is an interesting situation that I hadn't considered previously.  It looks like "=~" takes precedence over the "~" in the groovy parser.

So that in your example where you don't have a space between any of  "=~/", the groovy parser is picking "=~" over the "~/".

This demonstrates what's going on:

&lt;pre lang="groovy"&gt;
shoutedWord = "NASA"

// no space between = and ~ so it treats it as a =~
// returns a matcher object and doesn't modify shoutedWord
def noSpaceCreatesMatcher() { shoutedWord =~/\b[A-Z]+\b/ }

// space between = and ~ so this assigns the Pattern to shoutedWord 
def spaceAssignsPattern() { shoutedWord = ~/\b[A-Z]+\b/ }

assert "NASA" == shoutedWord
assert java.util.regex.Matcher == noSpaceCreatesMatcher().class
assert "NASA" == shoutedWord
assert java.util.regex.Pattern == spaceAssignsPattern().class
assert java.util.regex.Pattern == shoutedWord.class
assert ( /\b[A-Z]+\b/ == shoutedWord.toString() )
&lt;/pre&gt;

I guess the moral of the story is to be explicit where you put your spaces to insure you're getting the right intention.  If you want a matcher use "=~ /"  If you want a pattern, use "= ~/".</description>
		<content:encoded><![CDATA[<p>Thanks guys I&#8217;m glad you found the post useful!</p>
<p>@kodeninja that is an interesting situation that I hadn&#8217;t considered previously.  It looks like &#8220;=~&#8221; takes precedence over the &#8220;~&#8221; in the groovy parser.</p>
<p>So that in your example where you don&#8217;t have a space between any of  &#8220;=~/&#8221;, the groovy parser is picking &#8220;=~&#8221; over the &#8220;~/&#8221;.</p>
<p>This demonstrates what&#8217;s going on:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">shoutedWord <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;NASA&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// no space between = and ~ so it treats it as a =~</span>
<span style="color: #808080; font-style: italic;">// returns a matcher object and doesn't modify shoutedWord</span>
<span style="color: #000000; font-weight: bold;">def</span> noSpaceCreatesMatcher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> shoutedWord <span style="color: #66cc66;">=</span>~/\b<span style="color: #66cc66;">&#91;</span>A-Z<span style="color: #66cc66;">&#93;</span>+\b/ <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// space between = and ~ so this assigns the Pattern to shoutedWord </span>
<span style="color: #000000; font-weight: bold;">def</span> spaceAssignsPattern<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> shoutedWord <span style="color: #66cc66;">=</span> ~/\b<span style="color: #66cc66;">&#91;</span>A-Z<span style="color: #66cc66;">&#93;</span>+\b/ <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;NASA&quot;</span> <span style="color: #66cc66;">==</span> shoutedWord
<span style="color: #000000; font-weight: bold;">assert</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">regex</span>.<span style="color: #006600;">Matcher</span> <span style="color: #66cc66;">==</span> noSpaceCreatesMatcher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #000000; font-weight: bold;">class</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;NASA&quot;</span> <span style="color: #66cc66;">==</span> shoutedWord
<span style="color: #000000; font-weight: bold;">assert</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">regex</span>.<span style="color: #006600;">Pattern</span> <span style="color: #66cc66;">==</span> spaceAssignsPattern<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #000000; font-weight: bold;">class</span>
<span style="color: #000000; font-weight: bold;">assert</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">regex</span>.<span style="color: #006600;">Pattern</span> <span style="color: #66cc66;">==</span> shoutedWord.<span style="color: #000000; font-weight: bold;">class</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#40;</span> /\b<span style="color: #66cc66;">&#91;</span>A-Z<span style="color: #66cc66;">&#93;</span>+\b/ <span style="color: #66cc66;">==</span> shoutedWord.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I guess the moral of the story is to be explicit where you put your spaces to insure you&#8217;re getting the right intention.  If you want a matcher use &#8220;=~ /&#8221;  If you want a pattern, use &#8220;= ~/&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: codecraig</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-212</link>
		<dc:creator>codecraig</dc:creator>
		<pubDate>Tue, 20 May 2008 10:31:19 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-212</guid>
		<description>Great information, thanks for the post.</description>
		<content:encoded><![CDATA[<p>Great information, thanks for the post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kodeninja</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-209</link>
		<dc:creator>kodeninja</dc:creator>
		<pubDate>Tue, 20 May 2008 07:12:00 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-209</guid>
		<description>An interesting corner case that could come up here :)

def shoutedWord = "NASA"
def shoutedWordP = ~/\b[A-Z]+\b/ // creates a pattern
def shoutedWordM = "EUREKA" =~ /\b[A-Z]+\b/ // creates a matcher

shoutedWord =~/\b[A-Z]+\b/ // what does this create?

-kodeninja</description>
		<content:encoded><![CDATA[<p>An interesting corner case that could come up here :)</p>
<p>def shoutedWord = &#8220;NASA&#8221;<br />
def shoutedWordP = ~/\b[A-Z]+\b/ // creates a pattern<br />
def shoutedWordM = &#8220;EUREKA&#8221; =~ /\b[A-Z]+\b/ // creates a matcher</p>
<p>shoutedWord =~/\b[A-Z]+\b/ // what does this create?</p>
<p>-kodeninja</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Berkay</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-176</link>
		<dc:creator>Berkay</dc:creator>
		<pubDate>Mon, 19 May 2008 08:34:13 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-176</guid>
		<description>This is really helpful, thanks for taking the time to write it up!</description>
		<content:encoded><![CDATA[<p>This is really helpful, thanks for taking the time to write it up!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andres Almiray</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-174</link>
		<dc:creator>Andres Almiray</dc:creator>
		<pubDate>Mon, 19 May 2008 06:31:41 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-174</guid>
		<description>Great write up, keep'em coming!</description>
		<content:encoded><![CDATA[<p>Great write up, keep&#8217;em coming!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
