Regular expressions and dates

A regular expression in Groovy is a slashy string, and the operators of the operators lesson connect it to text. The date types are the ones of Java: LocalDate, Period and a formatter.

A pattern with groups

regex.groovy

def text = 'Ada Lovelace 1815-12-10' def matcher = text =~ /(\w+) (\w+) (\d{4})-(\d{2})-(\d{2})/ println matcher.matches() println matcher[0][1] println matcher[0][2] println matcher[0][3..5].join('-') println text.replaceAll(/\d{4}-\d{2}-\d{2}/, 'a date') println text.replaceAll(/(\w+) (\w+)/) { List<String> all, String first, String last -> "${last}, ${first}" }

groovy regex.groovy

true
Ada
Lovelace
1815-12-10
Ada Lovelace a date
Caught: groovy.lang.MissingMethodException: No signature of method: doCall for class: regex$_run_closure1 is applicable for argument types: (ArrayList) values: [[Ada Lovelace, Ada, Lovelace]]
Possible solutions: doCall(java.util.List, java.lang.String, java.lang.String), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)
groovy.lang.MissingMethodException: No signature of method: doCall for class: regex$_run_closure1 is applicable for argument types: (ArrayList) values: [[Ada Lovelace, Ada, Lovelace]]
Possible solutions: doCall(java.util.List, java.lang.String, java.lang.String), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)
at regex.run(regex.groovy:10)
exit status: 1

matcher[0] is the whole match and matcher[0][n] is group n, so the named parts of a line become a list without a second parse. replaceAll takes a string or a closure whose parameters are the whole match and the groups.

Dates

dates.groovy

import java.time.LocalDate import java.time.Period import java.time.format.DateTimeFormatter def born = LocalDate.parse('1815-12-10') println born println born.plusYears(36) println born.dayOfWeek println Period.between(born, LocalDate.of(2026, 1, 1)).years def formatter = DateTimeFormatter.ofPattern('d MMMM yyyy') println born.format(formatter)

groovy dates.groovy

1815-12-10
1851-12-10
SUNDAY
210
10 December 1815

LocalDate is a date without a time zone, which is the right type for a birthday and the wrong one for an instant. Period is a difference of dates in years, months and days, and a DateTimeFormatter turns one into text.

A date out of a line

line.groovy

import java.time.LocalDate import java.time.format.DateTimeFormatter def line = 'invoice 2026-11-24 total 19.98' def matcher = line =~ /(\d{4}-\d{2}-\d{2})/ def date = LocalDate.parse(matcher[0][1]) def amount = (line =~ /total (\d+\.\d+)/)[0][1] as BigDecimal println date.format(DateTimeFormatter.ISO_DATE) println date.plusDays(30) println amount * 2

groovy line.groovy

2026-11-24
2026-12-24
39.96

A pattern finds the two fields, and the typed parse follows: a date through LocalDate.parse and a number through as BigDecimal. Text in, typed values out is the whole job of a parser, and two small patterns are usually enough.

Related

Article author: Arthur Isaev