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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -