i have following regex:
string pattern = @"^(?<negate>!*)\[(?<value>.+?;.+?)\]$";
to parse strings "[2;8]" , "![2;8]". have:
match match = regex.match("[2;8]", pattern); if (match.success) { string[] values = match.groups["value"].value.split(';'); string minimum = values[0]; string maximum = values[1]; boolean negate = match.groups["negate"].value.length % 2 != 0; }
the problem regex not match "[;8]" or "[2;]".
how can change code can parse intervals min , max, min or max?
from test case, think may better efficiency using combination of string.split
, indexof
. i.e.
string input = "[2;8]"; bool negate = false; if(input[0] == '!'){ negate = true; input = input.substring(1); } string[] values = input.split(';'); string minimum = values[0]; string maximum = values[1];
however, simple modification of original pattern trick.
string pattern = @"^(?<negate>!*)\[(?<value>.*?;.*?)\]$";
the major problem here usage of '+?' requires there must single occurrence of value while '*?' means 0 or more.
Comments
Post a Comment