Tag: Groovy

3 Posts Found
 

Use Environment Variables in application.yml

Posted: 5/14/2017 9:29 AM

A convenient method of configuring parameters in a Grails app application.yml file is to use system environment variables instead of hard-coding values.

The syntax for an environment variable is:

settingToUse: ${VARIABLE_NAME_GOES_HERE}
 

Groovy Function Strips Non-Numeric Characters From a String

Posted: 11/27/2016 1:38 PM

Here's a simple regular expression function that will strip non-numeric characters from a string. Useful for cleaning phone numbers.

// strips non-numeric values from a string
def stripNonNumeric(String stringIn) {
    def returnString = stringIn.replaceAll(/[^0-9]/, "")
    return returnString
}
 

Groovy Function Tests If a String is a Valid Email Address

Posted: 11/26/2016 4:21 PM

Here's a simple regular expression function that will return a Boolean indicating if the specified string is an Email Address.

// is the string an email address
def isEmail(String emailAddress) {
    def emailRegEx = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2}$/

    def isEmail = !emailAddress.matches(emailRegEx)

    return isEmail
}