Multiline Regular Expression Option

I have a regex to match the comma date format.

yyyy / mm / dd or yyyy / mm

For instance:

2016/09 / 02,2016 / 08,2016 / 09/30

My code is:

string data="21535300/11/11\n"; Regex reg = new Regex(@"^(20\d{2}/(0[1-9]|1[012])(/(0[1-9]|[12]\d|30|31))?,?)*$", RegexOptions.Multiline); if (!reg.IsMatch(data)) "Error".Dump(); else "True".Dump(); 

I am using the multiline option. If the string data has "\ n". Any character will match this regular expression.

For instance:

 string data="test\n" string data="2100/1/1" 

I define a definition option in MSDN . It says:

It changes the interpretation of ^ and $ elements of the language so that they correspond to the beginning and end of the line instead of the beginning and end of the input line.

I did not understand why this problem occurred. Can anyone explain this? Thanks.

+5
source share
1 answer

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:

 ^ # Matches the start of a line ( # Start of Group 1 20\d{2}/ (0[1-9]|1[012]) (/ (0[1-9]|[12]\d|30|31) )?,? )* # End of group 1 - * quantifier makes it match 0+ times $ # End of line 

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.

+2
source

All Articles