locked
how to replace {} words in long rich text in c# RRS feed

  • Question

  • how to replace {} words in long rich text in c# easily

    For example

    Dear User,

    {Title} - {Description} for {CustomerName} has been Appproved by all Approvers. 

    Now I want to get all {} inside words(these are internal sp list column names and replace it with actual sharepoint list item value.


    MCTS Sharepoint 2010, MCAD dotnet, MCPDEA, SharePoint Lead

    Tuesday, July 19, 2016 8:18 PM

Answers

All replies

  • You can use the following regex pattern to replace the contents. 

    \{([^\}]+)\}

    Sample Code

    		string myString = "{Title} - {Description} for {CustomerName} has been Appproved by all Approvers.";
    
    
    		Regex yourRegex = new Regex(@"\{([^\}]+)\}");
    		myString = yourRegex.Replace(myString, "MY TITLE", 1);
    		myString = yourRegex.Replace(myString, "MY DESCRIPTION", 1);
    		myString = yourRegex.Replace(myString, "MY CUSTOMER NAME", 1);
    		
    		Console.WriteLine(myString);

    Refer to the fiddle below

    https://dotnetfiddle.net/7bMjGt


    Cheers,
    My Blog

    Visual Studio Developer Tools Download

    Tuesday, July 19, 2016 11:34 PM
  • thanks but this needs to be dynamic as I have many messages. So I want to extract all {} and match them with list column intername name and replace it with list item value.

    MCTS Sharepoint 2010, MCAD dotnet, MCPDEA, SharePoint Lead

    Wednesday, July 20, 2016 2:08 AM
  • Then this should help

    using System;
    using System.Text.RegularExpressions;
    					
    public class Program
    {
    	public static void Main()
    	{
    		string myString = "{Title} - {Description} for {CustomerName} has been Appproved by all Approvers.";
    		
    		string[] myStrings = new string[100];
    
    		Regex yourRegex = new Regex(@"\{([^\}]+)\}");
    		MatchCollection mc = yourRegex.Matches(myString);
    		foreach(Match match in mc)	
    			Console.WriteLine(match);
    	}
    }

    You need to write code to loop through the list with the matches found and get the item values. Hope it helps


    Cheers,
    My Blog

    Visual Studio Developer Tools Download

    Wednesday, July 20, 2016 3:23 AM
  • thank you!

    MCTS Sharepoint 2010, MCAD dotnet, MCPDEA, SharePoint Lead

    Wednesday, July 20, 2016 3:46 PM
  • Now I can make it work for plain text but we have rich text body field and for that I get

    p>Dear Submitter,<br style="box-sizing&#58;border-box;color&#58;#333333;font-family&#58;'Open Sans', sans-serif;font-size&#58;12px;"><br style="box-sizing&#58;border-box;color&#58;#333333;font-family&#58;'Open Sans', sans-serif;font-size&#58;12px;">&#123;Title&#125; - &#123;Description&#125; for &#123;CustomerName&#125; has

    Now how to replace &#123;(this is { in UI) using regex?


    MCTS Sharepoint 2010, MCAD dotnet, MCPDEA, SharePoint Lead

    Wednesday, July 20, 2016 5:51 PM
  • found it! encodehtml

    MCTS Sharepoint 2010, MCAD dotnet, MCPDEA, SharePoint Lead

    Thursday, July 21, 2016 3:50 PM
  • Good, sharing it here so other might find it helpful.

    	public static string StripHTML(string HTMLText, bool decode = true)
    	{
    		Regex reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
    		var stripped = reg.Replace(HTMLText, "");
    		return decode ? HttpUtility.HtmlDecode(stripped) : stripped;
    	}
    Fiddle: https://dotnetfiddle.net/8UrJbf

    Refer: 

    http://stackoverflow.com/questions/731649/how-can-i-convert-html-to-text-in-c

    Cheers,
    My Blog

    Visual Studio Developer Tools Download

    Thursday, July 21, 2016 6:54 PM