I’m writing a script that validates a file with key/value-pairs. One type of a valid value is a JNDI-name. Before the actual name it is also a whitespace. So my if-statement with the regex validating ended up like this

currentLine ==~ /^.*=\s+[A-Za-z0-9\/-]+/

To explain it:

  • ==~ means validate true/false, see http://groovy.codehaus.org/Tutorial+4+-+Regular+expressions+basics
  • Then the regex starts and ends with a forward slash
  • ^.*= means match lines with text containing any type of character ending with an equal sign (the keys)
  • \s+ means match one or more whitespaces. Should really just need to verify one and only one, but what the heck…
  • [A-Za-z0-9\/-]+ is a character class saying match all uppercase letters, all lowercase, all numbers and also forward slashes and hyphens. Some of the names have hyphens so I needed that one to. The + sign at the end says “one or more occurences”, see the heading “Regular Expression Operators” found on the above mentioned link