Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Regex.Escape()
- Regex.Escape()
- Regex.Escape()
- Regex.Escape()
- string abbr = "memb";
- string word = "Member";
- string pattern = String.Format("b{0}b", Regex.Escape(abbr));
- string substitue = String.Format("[a title="{0}"]{1}[/a]", word, abbr);
- string output = Regex.Replace(input, pattern, substitue);
- b(?:{abbr_1}|{abbr_2}|{abbr_3}|{abbr_n})b
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input = @"This is just a little test of the memb to see if it gets picked up.
- Deb of course should also be caught here.";
- var dictionary = new Dictionary<string,string>
- {
- {"memb", "Member"}
- ,{"deb","Debut"}
- };
- var regex = "(" + String.Join(")|(", dictionary.Keys.ToArray()) + ")";
- foreach (Match metamatch in Regex.Matches(input
- , regex /*@"(memb)|(deb)"*/
- , RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture))
- {
- input = input.Replace(metamatch.Value, dictionary[metamatch.Value.ToLower()]);
- }
- Console.Write (input);
- Console.ReadLine();
- }
- }
- }
- var abbrsWithPipes = "(abbr1|abbr2)";
- var regex = new Regex(abbrsWithPipes);
- return regex.Replace(html, m => GetReplaceForAbbr(m.Value));
- public partial class Abbreviations : System.Web.UI.UserControl
- {
- private Dictionary<String, String> 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("<b title='{0}'>{1}</b>", dictionary[m.Value.ToLower()], m.Value);
- }
- }
- This is just a little test of the <b title='Member'>memb</b>. And another <b title='Member'>memb</b>, but not amemba to see if it gets picked up. <b title='Debut'>Deb</b> of course should also be caught here.<b title='Debut'>deb</b>!
- public static string GetGlossaryString(string str)
- {
- List<string> glossaryWords = GetGlossaryItems();//this collection would contain your abbreviations; you could just make it a Dictionary so you can have the abbreviation-full term pairs and use them in the loop below
- str = string.Format(" {0} ", str);//quick and dirty way to also search the first and last word in the content.
- foreach (string word in glossaryWords)
- str = Regex.Replace(str, "([\W])(" + word + ")([\W])", "$1<span class='glossaryItem'>$2</span>$3", RegexOptions.IgnoreCase);
- return str.Trim();
- }
Add Comment
Please, Sign In to add comment