c# - Regular expression to match non escaped character -


i need regex match keyword's value , optional type possible conditions following.

in examples below curly braced fields need capture. somevaluexxx expected there although there might rare cases value null/empty string. type=xxxx optional argument , might not there.

the closest regular expression able produce following: ^anniversary(?:(?::)|(?:;.*:))([^:]*)$ capture anniversary's value fail when there's escaped colon (/:) in value.

somekeyword:{somevalue}  somekeyword:{somevalue/:withcolonescaped}  somekeyword:{somevalue/:withsemicolonescaped}  somekeyword;type={sometype}:{somevalue}  somekeyword;type={sometype}:{somevalue/:withcolonescaped}  somekeyword;type={sometype}:{somevalue/;withsemicolonescaped}  somekeyword;arg1=myarg1;type={sometype}:{somevalue}  somekeyword;arg1=myarg1;type={sometype}:{somevalue/:withcolonescaped}  somekeyword;arg1=myarg1;type={sometype}:{somevalue/;withsemicolonescaped}  somekeyword;arg1=myarg1;type={sometype};arg2=myarg2:{somevalue}  somekeyword;arg1=myarg1;type={sometype};arg2=myarg2:{somevalue/:withcolonescaped}  somekeyword;arg1=myarg1;type={sometype};arg2=myarg2:{somevalue/;withsemicolonescaped} 

left curly braces intact demonstration, when removed gives desired results:

var testing = new string[] {     "somekeyword:{somevalue}",     "somekeyword:{somevalue/:withcolonescaped}",     "somekeyword:{somevalue/;withsemicolonescaped}",     "somekeyword;type={sometype}:{somevalue}",     "somekeyword;type={sometype}:{somevalue/:withcolonescaped}",     "somekeyword;type={sometype}:{somevalue/;withsemicolonescaped}",     "somekeyword;arg1=myarg1;type={sometype}:{somevalue}",     "somekeyword;arg1=myarg1;type={sometype}:{somevalue/:withcolonescaped}",     "somekeyword;arg1=myarg1;type={sometype}:{somevalue/;withsemicolonescaped}",     "somekeyword;arg1=myarg1;type={sometype};arg2=myarg2:{somevalue}",     "somekeyword;arg1=myarg1;type={sometype};arg2=myarg2:{somevalue/:withcolonescaped}",     "somekeyword;arg1=myarg1;type={sometype};arg2=myarg2:{somevalue/;withsemicolonescaped}" };  // tried use fewest number of capture groups readability var regex = new regex(     @"         (             (type=(?<type>[^;]+);[^:]*?)                     |              (type=(?<type>.*?))         )?         :         (?<value>.*)$     ",     regexoptions.compiled      | regexoptions.ignorecase     | regexoptions.ignorepatternwhitespace );  foreach (var test in testing) {     match match = regex.match(test);     console.write(         "type: [{0}] || value: [{1}]\n",         match.groups["type"].value,         match.groups["value"].value     ); } 

if case matters remove regexoptions.ignorecase.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -