<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<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>
	<lastBuildDate>Sat, 31 Jul 2010 11:43:17 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/comment-page-1/#comment-2046</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Fri, 30 Oct 2009 05:25:15 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-2046</guid>
		<description>@Nikolas

You can use gstrings in the patterns just like in any other string, so for your example, this should work:

&lt;pre lang=&quot;groovy&quot;&gt;
def myparam = &quot;([1][2][3])&quot;
def pattern = ~&quot;$myparam ([0-9/-]+)([0-9/-]+)&quot;

evp = &quot;123 456&quot;

// asserts just show what will be matched for example string
// returns the group matching what&#039;s in myparam
evp.find(pattern) { full, matchedMyParam, firstDigitGroup, secondDigitGroup -&gt;
    assert full == &quot;123 456&quot;
    assert matchedMyParam == &quot;123&quot;
    assert firstDigitGroup == &quot;45&quot;
    assert secondDigitGroup == &quot;6&quot;
    return matchedMyParam
}
&lt;/pre&gt;

Hope that helps!</description>
		<content:encoded><![CDATA[<p>@Nikolas</p>
<p>You can use gstrings in the patterns just like in any other string, so for your example, this should work:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> myparam <span style="color: #669966;">=</span> <span style="color: #aa0000;">&quot;([1][2][3])&quot;</span>
<span style="color: #000000; font-weight: bold;">def</span> pattern <span style="color: #669966;">=</span> ~<span style="color: #aa0000;">&quot;$myparam ([0-9/-]+)([0-9/-]+)&quot;</span>
&nbsp;
evp <span style="color: #669966;">=</span> <span style="color: #aa0000;">&quot;123 456&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// asserts just show what will be matched for example string</span>
<span style="color: #808080; font-style: italic;">// returns the group matching what's in myparam</span>
evp.<span style="color: #663399;">find</span><span style="color: #669966;">&#40;</span>pattern<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> full, matchedMyParam, firstDigitGroup, secondDigitGroup <span style="color: #669966;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> full <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;123 456&quot;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> matchedMyParam <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;123&quot;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> firstDigitGroup <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;45&quot;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> secondDigitGroup <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;6&quot;</span>
    <span style="color: #000000; font-weight: bold;">return</span> matchedMyParam
<span style="color: #669966;">&#125;</span></pre></div></div>

<p>Hope that helps!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nikolas</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/comment-page-1/#comment-2043</link>
		<dc:creator>Nikolas</dc:creator>
		<pubDate>Thu, 29 Oct 2009 21:55:29 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-2043</guid>
		<description>Hi I need to include a parameter / String variable/ in a Matcher pattern so then the Groovy script could search dinamicaly for my needs, Could you suggest how to do that ? Should look like this :
 gmatcher = evp =~ &quot;myparam ([0-9/-]+)([0-9/-]+)&quot;   where 
my param = ([1] [2] [3])

Any help is grealtly apreciated.

Thanks:
Nikolas</description>
		<content:encoded><![CDATA[<p>Hi I need to include a parameter / String variable/ in a Matcher pattern so then the Groovy script could search dinamicaly for my needs, Could you suggest how to do that ? Should look like this :<br />
 gmatcher = evp =~ &#8220;myparam ([0-9/-]+)([0-9/-]+)&#8221;   where<br />
my param = ([1] [2] [3])</p>
<p>Any help is grealtly apreciated.</p>
<p>Thanks:<br />
Nikolas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/comment-page-1/#comment-1255</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Sun, 08 Mar 2009 01:14:12 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-1255</guid>
		<description>@Steve 

Your statement regarding replaceAll: &quot;does it replace the whole match, and you have to construct it out of the parameters representing the groups&quot; is the correct one.

The entire thing that was matched by the regexp statement is replaced by whatever is returned.  You just have access to the groups to help you parse out the different pieces to help you construct the updated string.  A couple of examples:

without groups, it replaces the whole match:
&lt;pre lang=&quot;groovy&quot;&gt;
assert &quot;foo qux qux&quot; == &quot;foo bar baz&quot;.replaceAll(/b../) { wholeMatch -&gt;
   return &quot;qux&quot;
}
&lt;/pre&gt;

with groups, you can use the groups to help you construct the result, but it still replaces the whole match (and leaves the start of the string, &quot;foo&quot;, untouched):
&lt;pre lang=&quot;groovy&quot;&gt;
assert &quot;foo car caz&quot; == &quot;foo bar baz&quot;.replaceAll(/b(..)/) { wholeMatch, lastTwoChars -&gt;
   return &quot;c&quot; + lastTwoChars
}
&lt;/pre&gt;

Hope that helps!</description>
		<content:encoded><![CDATA[<p>@Steve </p>
<p>Your statement regarding replaceAll: &#8220;does it replace the whole match, and you have to construct it out of the parameters representing the groups&#8221; is the correct one.</p>
<p>The entire thing that was matched by the regexp statement is replaced by whatever is returned.  You just have access to the groups to help you parse out the different pieces to help you construct the updated string.  A couple of examples:</p>
<p>without groups, it replaces the whole match:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;foo qux qux&quot;</span> <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;foo bar baz&quot;</span>.<span style="color: #006600;">replaceAll</span><span style="color: #669966;">&#40;</span>/b../<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> wholeMatch <span style="color: #669966;">-&gt;</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #aa0000;">&quot;qux&quot;</span>
<span style="color: #669966;">&#125;</span></pre></div></div>

<p>with groups, you can use the groups to help you construct the result, but it still replaces the whole match (and leaves the start of the string, &#8220;foo&#8221;, untouched):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;foo car caz&quot;</span> <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;foo bar baz&quot;</span>.<span style="color: #006600;">replaceAll</span><span style="color: #669966;">&#40;</span>/b<span style="color: #669966;">&#40;</span>..<span style="color: #669966;">&#41;</span>/<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> wholeMatch, lastTwoChars <span style="color: #669966;">-&gt;</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #aa0000;">&quot;c&quot;</span> <span style="color: #669966;">+</span> lastTwoChars
<span style="color: #669966;">&#125;</span></pre></div></div>

<p>Hope that helps!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/comment-page-1/#comment-1253</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Wed, 04 Mar 2009 17:08:36 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-1253</guid>
		<description>Thanks for the great info, much better than my Groovy book.

One thing I&#039;m still confused by -- for replaceAll with a closure, you get pass the whole match, and then all the groups, right? And it seems from your example that the closure&#039;s result replaces the first group if there are groups? 

What if there&#039;s more than one group? How can I replace individual ones? Or does it replace the whole match, and you have to construct it out of all the parameters representing the groups?

An example for that would be great. 

Thanks!</description>
		<content:encoded><![CDATA[<p>Thanks for the great info, much better than my Groovy book.</p>
<p>One thing I&#8217;m still confused by &#8212; for replaceAll with a closure, you get pass the whole match, and then all the groups, right? And it seems from your example that the closure&#8217;s result replaces the first group if there are groups? </p>
<p>What if there&#8217;s more than one group? How can I replace individual ones? Or does it replace the whole match, and you have to construct it out of all the parameters representing the groups?</p>
<p>An example for that would be great. </p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/comment-page-1/#comment-1251</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Sat, 28 Feb 2009 14:44:00 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-1251</guid>
		<description>This was helpful.

Thanks</description>
		<content:encoded><![CDATA[<p>This was helpful.</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/comment-page-1/#comment-1216</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Thu, 29 Jan 2009 22:37:52 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=23#comment-1216</guid>
		<description>Just have to say that I keep coming back to this post over and over for reference when I&#039;m dealing with regex.  Thanks for the great examples!</description>
		<content:encoded><![CDATA[<p>Just have to say that I keep coming back to this post over and over for reference when I&#8217;m dealing with regex.  Thanks for the great examples!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/comment-page-1/#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&#039;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=&quot;groovy&quot;&gt;
matcher = ( &quot;test1 test2&quot; =~ /test([0-9]+)/ )
matcher.each { fullMatch, number -&gt;
    println &quot;full match: $fullMatch, number: $number&quot;
}
&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" style="font-family:monospace;">matcher <span style="color: #669966;">=</span> <span style="color: #669966;">&#40;</span> <span style="color: #aa0000;">&quot;test1 test2&quot;</span> <span style="color: #669966;">=</span>~ /test<span style="color: #669966;">&#40;</span><span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #669966;">-</span><span style="color: #cc66cc;">9</span><span style="color: #669966;">&#93;</span><span style="color: #669966;">+</span><span style="color: #669966;">&#41;</span>/ <span style="color: #669966;">&#41;</span>
matcher.<span style="color: #663399;">each</span> <span style="color: #669966;">&#123;</span> fullMatch, number <span style="color: #669966;">-&gt;</span>
    <span style="color: #663366;">println</span> <span style="color: #aa0000;">&quot;full match: $fullMatch, number: $number&quot;</span>
<span style="color: #669966;">&#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-page-1/#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 :

&quot;A limitation of the iterator-based methods is that they don’t give you access to the individual groups&quot;

why doesn&#039;t this work (why Is x a sting instead of list )  :

matcher = ( &quot;test1 test2&quot; =~ /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-page-1/#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>[...] 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 [...]</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-page-1/#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>[...] Don&#8217;t fear the RegExp &#8211; new features in Groovy regular expressions. [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
