Option 1:
Use capturing groups to break up the values, then check the values inside the capturing groups if they match a valid date:
^([\w]{4})([\w]{2})([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{8})$
This will match something like: ABCDZY1999123101234567
into the following groups:
(ABCD)(ZY)(1999)(12)(31)(01234567)
^ ^ ^ ^ ^ ^
| | | | | |
| | | | | group 5
| | | | group 4
| | | group 3
| | group 2
| group 1
group 0
In this case, groups 2, 3, and 4 would compose the date components. You can experiment and validate this regular expression through one of the many online RegEx testers, like regex101.com.
Option 2:
Take the above approach, but get more creative with the number capture groups to include only the date ranges desired. See Matching Numeric Ranges with a Regular Expression:
examples from the above site:
- 000..255:
^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$
- 0 or 000..255:
^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$
- 0 or 000..127:
^(0?[0-9]?[0-9]|1[01][0-9]|12[0-7])$
- 0..999:
^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$
However, this approach is very difficult to maintain and should only be used if you cannot use option 1 alone.