.NET Framework Bookmark and Share   
 index > Regular Expressions > Need help finding and replacing text for "{#ANY_TEXT#}"
 

Need help finding and replacing text for "{#ANY_TEXT#}"

Hi,

I need your expert help. I'm trying to find a regular expression to replace all text between quotes "{#ANY_TEXT#}"

For example, if I have string like "This is a test {#ANY_TEXT#} string". I need to replace everything starting from {#...#} with another value. For example, If I want to make it blank, then end result should be "This is a test string". There could be several instances of this.

Thanks in advance for your help.

Andrew

AndrewLWong

Since this may leave an extra space, can we assume that {#ANY_TEX#} will never be at the start of string or at the end? Or can it be?. In other words we can also do something like this in your example to eliminate that.

String word = "This is a test {#ANY_TEXT#} string";
String pattern = @"\{[^\}]*\}\s(\w)";
word = Regex.Replace(word, pattern, "$1");
Console.WriteLine(word);
Console.ReadLine();

John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Proposed As Answer byAhmad Mageed Tuesday, September 15, 2009 6:16 PM
  •  
JohnGrove
This is good enough for me. Thanks so much for your help and quick response!!!!
AndrewLWong
Or:


String word = "{#ANY_TEXT#} This is a test {#ANY_TEXT#} string {#ANY_TEXT#}";
String pattern = @"\s?\{[^\}]*\}";
word = Regex.Replace(word, pattern, "");
Console.WriteLine(word.Trim());
Console.ReadLine();

John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove
Can we close this thread?
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove

You can use google to search for other answers

Custom Search

More Threads

• app.config update
• RE for a not null decimal
• VS2005's Search & Replace RegEx support is crippled?
• help building a regular expression
• What is the regular expression to find two word on a sentense.
• parse rows of a table / newline problem
• Retrieve html
• Regex uppercase words
• how i can cancel a regex.ismatch() when it's working
• Deduce Regex Informations