c# - Regex to match exact words in double square brackets -
i have following regex, matches words inside double bracket squares:
@"(?<=\[\[)[^]]+(?=\])"
problem: want replace in input
[[hello]] -> foo [[helloworld]] -> bar
code following:
message = message.replace(match.value, value.tostring()); message = regex.replace(message, @"[\[\]']+", "");
in output, receive fooworld. how should modify regex foo , bar?
your regex @"(?<=\[\[)[^]]+(?=\])"
matches 1+ characters other ]
if preceded [[
, followed ]
. not match [[x x x]]
-like strings. not match [[x y ] bracket ]]
-like strings.
you achieve need @"\[\[(.*?)]]"
regex (using regexoptions.singleline
flag), , replacing $1
:
message = regex.replace("[[hello]]", @"\[\[(.*?)]]", "$1"));
see ideone demo
however, given current requirements (or absence) can use
message = message.replace("[[", "").replace("]]", "");
Comments
Post a Comment