Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For anyone interested, here is my final solution. It is for a .NET user control. It uses a single pattern with a match evaluator, as suggested by Tomalak, so there is no foreach loop. It's an elegant solution, and it gives me the correct output for the sample input while preserving correct casing for matched strings.</p> <pre><code>public partial class Abbreviations : System.Web.UI.UserControl { private Dictionary&lt;String, String&gt; dictionary = DataHelper.GetAbbreviations(); protected void Page_Load(object sender, EventArgs e) { string input = "This is just a little test of the memb. And another memb, but not amemba to see if it gets picked up. Deb of course should also be caught here.deb!"; var regex = "\\b(?:" + String.Join("|", dictionary.Keys.ToArray()) + ")\\b"; MatchEvaluator myEvaluator = new MatchEvaluator(GetExplanationMarkup); input = Regex.Replace(input, regex, myEvaluator, RegexOptions.IgnoreCase); litContent.Text = input; } private string GetExplanationMarkup(Match m) { return string.Format("&lt;b title='{0}'&gt;{1}&lt;/b&gt;", dictionary[m.Value.ToLower()], m.Value); } } </code></pre> <p>The output looks like this (below). Note that it only matches full words, and that the casing is preserved from the original string:</p> <pre><code>This is just a little test of the &lt;b title='Member'&gt;memb&lt;/b&gt;. And another &lt;b title='Member'&gt;memb&lt;/b&gt;, but not amemba to see if it gets picked up. &lt;b title='Debut'&gt;Deb&lt;/b&gt; of course should also be caught here.&lt;b title='Debut'&gt;deb&lt;/b&gt;! </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload