Discussion:
[groovy-user] Accessing a regex Pattern group from a switch/case
Grey, Lee
2011-02-22 17:44:37 UTC
Permalink
I'd like to do something like this:

def s = 'tracking_level_2'

switch(s) {
case ~/tracking_level_\d/:
assert '2' == ______
}

Is there anything that can fill in the blank above that gives me access to the digit at the end of the Pattern?

Thanks,
Lee Grey
Jason Davis
2011-02-22 18:49:36 UTC
Permalink
i do something similar by loading Patterns in a list then

patterns.find { it ==~ someStr }
Post by Grey, Lee
def s = 'tracking_level_2'
switch(s) {
  assert '2' == ______
}
Is there anything that can fill in the blank above that gives me access to
the digit at the end of the Pattern?
Thanks,
Lee Grey
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Jason Davis
2011-02-22 18:53:27 UTC
Permalink
and combine that with a strategy pattern

strats[starts.keySet.find { it ==~ someStr }]()

where starts is a map with the pattern as the keys and a default
closure for null
Post by Jason Davis
i do something similar by loading Patterns in a list then
patterns.find { it ==~ someStr }
Post by Grey, Lee
def s = 'tracking_level_2'
switch(s) {
  assert '2' == ______
}
Is there anything that can fill in the blank above that gives me access to
the digit at the end of the Pattern?
Thanks,
Lee Grey
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Roshan Dawrani
2011-02-23 03:30:09 UTC
Permalink
*def s = 'tracking_level_2'*
**
*switch(s) {
assert '2' == ______
}*
Is there anything that can fill in the blank above that gives me access to
the digit at the end of the Pattern?
See if it helps: "Getting handle to matcher inside switch
case"<http://groovyconsole.appspot.com/script/424001>
Roshan Dawrani
2011-02-23 03:49:15 UTC
Permalink
*def s = 'tracking_level_2'*
**
*switch(s) {
assert '2' == ______
}*
Is there anything that can fill in the blank above that gives me access to
the digit at the end of the Pattern?
Just turned it into a small post as well: "Groovy Tip: Accessing Pattern
Matcher instance Used in Switch Case
Statements"<http://roshandawrani.wordpress.com/2011/02/23/groovy-tip-accessing-matcher-used-in-pattern-matching-in-switch-case-statements/>
as
it may help someone later.
Guillaume Laforge
2011-02-23 13:10:06 UTC
Permalink
Using Matcher.lastMatcher might be a bit better.
It's a DGSM method added on matcher, so it's more "public" API compared to
using the org.codehaus.groovy.runtime class which is more "internal" API.

(I added a comment on the Groovy Web Console example)

Anyhow, that's a nice trick, and I didn't even remember about that
RegexSupport stuff!
Funny how I can still (re)discover certain nuggets like that after all this
time :-)

Guillaume
Post by Roshan Dawrani
*def s = 'tracking_level_2'*
**
*switch(s) {
assert '2' == ______
}*
Is there anything that can fill in the blank above that gives me access
to the digit at the end of the Pattern?
Just turned it into a small post as well: "Groovy Tip: Accessing Pattern
Matcher instance Used in Switch Case Statements"<http://roshandawrani.wordpress.com/2011/02/23/groovy-tip-accessing-matcher-used-in-pattern-matching-in-switch-case-statements/> as
it may help someone later.
--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Loading...