Your regex may match the empty string you get after adding a new line at the end of the line. "test\n" contains 2 lines, and the second one matches.
See the regex pattern in free space mode:
^
If you do not want it to match the empty string, replace the last one )* with )+ .
An alternative is to use a more detailed template, for example
^20\d{2}/(0[1-9]|1[012])(/(0[1-9]|[12]\d|3[01]))?(,20\d{2}/(0[1-9]|1[012])(/(0[1-9]|[12]\d|3[01]))?)*$
See the demo of regex . Inside the code, it is recommended to use a block and dynamically build a template:
string date = @"20\d{2}/(0[1-9]|1[012])(/(0[1-9]|[12]\d|3[01]))?"; Regex reg = new Regex(string.Format("^{0}(,{0})*$", date), RegexOptions.Multiline);
As you can see, the first block (after the start of the ^ anchor line) is required here, and therefore an empty line will never be matched.
source share