Regular Expressions 3/3

//pattern examples. Creating a time pattern that can be reused.
def timePattern = ~/dd:dd/

def timeString = "Between 06:00 and 10:00, most geeks are asleep."
def matcher = timePattern.matcher(timeString)
def times = []
matcher.each { times << it }
assert times.join(", ") == '06:00, 10:00'

def anotherTimeString = "But between 22:00 and 02:00, most geeks are awake"
//we reuse the pattern
matcher = timePattern.matcher(anotherTimeString)
times = []
matcher.each { times << it }
assert times.join(", ") == '22:00, 02:00'

source

Leave a Reply