c# - Regex to include and exclude by multi characters -
i have string want remove negative values example,
var mystring = "+colour:black +year:2015 -model:golf"; i tried using following regex doesn't work.
var reg = "[+a-z:0-9]";
you can use
var result = regex.replace(mystring, @"\s*[-][^\s]*\s*", string.empty); for positive values:
var result = regex.replace(mystring, @"\s*[+][^\s]*\s*", string.empty); for both positive , negative:
var result = regex.replace(mystring, @"\s*[+-][^\s]*\s*", string.empty); this way, handle stray hyphens no characters whitespace after them, , \s* trim output string remanining spaces.
notes on regex: [-+] matches either - or + once, \s* matches 0 or more whitespace, , [^\s]* matches 0 or more characters other whitespace.
see demo

Comments
Post a Comment