Replace text in string with delimeters using Regex
Posted
by
user1057735
on Stack Overflow
See other posts from Stack Overflow
or by user1057735
Published on 2011-11-21T12:07:13Z
Indexed on
2011/11/21
17:50 UTC
Read the original article
Hit count: 273
I have a string something like,
string str = "(50%silicon +20%!(20%Gold + 80%Silver)| + 30%Alumnium)";
I need a Regular Expression which would Replace the contents in between ! and | with an empty string. The result should be (50%silicon +20% + 30%Alumnium)
.
If the string contains something like (with nested delimiters):
string str = "(50%silicon +20%!(80%Gold + 80%Silver + 20%!(20%Iron + 80%Silver)|)|
+ 30%Alumnium)";
The result should be (50%silicon +20% + 30%Alumnium)
- ignoring the nested delimiters.
I've tried the following Regex, but it doesn't ignore the nesting:
Regex.Replace(str , @"!.+?\|", "", RegexOptions.IgnoreCase);
© Stack Overflow or respective owner