Using Groovy, I\'d like to generate a random sequence of characters from a given regular expression.
[A-Z0-9]
import org.apache.commons.lang.RandomStringUtils
String charset = (('A'..'Z') + ('0'..'9')).join()
Integer length = 9
String randomString = RandomStringUtils.random(length, charset.toCharArray())
The imported class RandomStringUtils
is already on the Grails classpath, so you shouldn't need to add anything to the classpath if you're writing a Grails app.
If you only want alphanumeric characters to be included in the String you can replace the above with
String randomString = org.apache.commons.lang.RandomStringUtils.random(9, true, true)