Skip to content

Instantly share code, notes, and snippets.

@marcgeld
Created June 13, 2017 08:12
Show Gist options
  • Save marcgeld/a5af999a0360819fa4a3f74aed7ea221 to your computer and use it in GitHub Desktop.
Save marcgeld/a5af999a0360819fa4a3f74aed7ea221 to your computer and use it in GitHub Desktop.
Groovy: regex & Base64
#!/usr/bin/env groovy
import java.util.Base64
import java.util.Base64.Decoder
import java.util.Base64.Encoder
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.nio.charset.StandardCharsets;
final Decoder decoder = Base64.getDecoder()
String bytesEncoded = Base64.getEncoder().encodeToString("a test message to encode".getBytes( StandardCharsets.UTF_8 ) )
String line = "=[" + bytesEncoded + "]"
println "value before regex: ${line}"
Pattern r = Pattern.compile( "=\\[(.*)\\]" )
Matcher m = r.matcher( line )
if ( m.find( ) ) {
String tmp = m.group( 1 )
println "value after rexex find: ${tmp}"
byte[] decodedTmp = decoder.decode( tmp )
def val = new String(decodedTmp, StandardCharsets.UTF_8);
println "value after decoding: ${val}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment