smeeb smeeb - 1 month ago 6
Groovy Question

Extracting user-defined Groovy tokens from strings

Groovy here : I need to scan a string for a substring of the form:

${token}:<someValue>]


That is:


  • A user-define (dynamic)
    token
    string (could be anything at runtime); then

  • A colon (
    :
    ); then

  • Anything (
    <someValue>
    ); then finally

  • A right squre bracket (
    ]
    )



So basically something like:

def String fetchTokenValue(String toScan, String token) {
if(toScan.matches(".*${token}:.*]")) {
String everythingBetweenColonAndRBracket = ???
return everythingBetweenColonAndRBracket
} else {
return 'NO_DICE'
}
}


Such that the output would be as follows:

fetchTokenValue('swkokd sw:defroko swodjejr blah:fizzbuzz] wdkerko', 'blah') => 'fizzbuzz'
fetchTokenValue('swkokd sw:defroko swodjejr blah:fizzbuzz] wdkerko', 'boo') => 'NO_DICE'


I'm struggling with the regex as well as how to, if a match is made, extract all the text between the colon and the right square bracket. We can assume there will only ever be one match, or simply operate on the first match that is found (if it exists).

Any ideas where I'm going awry?

Answer

You may use [^\]]* subpattern (a negated character class [^...] that matches any chars other than those defined inside it) to match zero or more chars other than ] and use a capturing group to capture that text and only return Group 1 contents. Also, it is a good idea to automatically escape the input token so as to avoid illegal pattern syntax issues:

import java.util.regex.*;
def String fetchTokenValue(String toScan, String token) {
    def matcher = ( toScan =~ /.*${Pattern.quote(token)}:([^\]]*)].*/ )
    if(matcher.matches()) {
        return matcher.group(1)
    } else {
        return 'NO_DICE'
    }
}
println fetchTokenValue('swkokd sw:defroko swodjejr blah:fizzbuzz] wdkerko', 'blah')

See the online Groovy demo

Comments