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

enter image description here


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -