Search code examples
javaregexisodate

RegEx for validating correct ISO8601 date string


For example: 2013-08-11T17:22:04.51+01:00

In this stackoverflow answer cover ISODateTime without .51 part.

Please help to correct this regex

^(?:[1-9]\d{3}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\d|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)-02-29)T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:Z|[+-][01]\d:[0-5]\d)$

to handle my format.


Solution

  • Using capturing groups, you can simply design an expression to capture anything that you wish from your input. For example this expression,

    (\d{4}-\d{2}-\d{2})[A-Z]+(\d{2}:\d{2}:\d{2}).([0-9+-:]+)
    

    divides your input into three capturing groups and you can simply call them using $1-$3.

    You can also add any char that you may want to in [].

    enter image description here

    RegEx Descriptive Graph

    This graph shows how the expression would work and you can visualize other expressions in this link:

    enter image description here

    Java Test

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    final String regex = "(\\d{4}-\\d{2}-\\d{2})[A-Z]+(\\d{2}:\\d{2}:\\d{2}).([0-9+-:]+)";
    final String string = "2013-08-11T17:22:04.51+01:00";
    final String subst = "\\1 \\2 \\3";
    
    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);
    
    // The substituted value will be contained in the result variable
    final String result = matcher.replaceAll(subst);
    
    System.out.println("Substitution result: " + result);
    

    JavaScript Demo

    const regex = /(\d{4}-\d{2}-\d{2})[A-Z]+(\d{2}:\d{2}:\d{2}).([0-9+-:]+)/gm;
    const str = `2013-08-11T17:22:04.51+01:00`;
    const subst = `\nGroup 1: $1\nGroup 2: $2\nGroup 3: $3\n`;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);

    Basic Performance Test

    This JavaScript snippet shows the expression performance using a simple 1-million times for loop.

    const repeat = 1000000;
    const start = Date.now();
    
    for (var i = repeat; i >= 0; i--) {
    	const string = '2013-08-11T17:22:04.51+01:00';
    	const regex = /(\d{4}-\d{2}-\d{2})[A-Z]+(\d{2}:\d{2}:\d{2}).([0-9+-:]+)/gm;
    	var match = string.replace(regex, "\nGroup #1: $1 \n Group #2: $2 \n Group #3: $3 \n");
    }
    
    const end = Date.now() - start;
    console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
    console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");