Guest User

Untitled

a guest
Jun 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. Regex.Escape()
  2.  
  3. Regex.Escape()
  4.  
  5. Regex.Escape()
  6.  
  7. Regex.Escape()
  8.  
  9. string abbr = "memb";
  10. string word = "Member";
  11. string pattern = String.Format("b{0}b", Regex.Escape(abbr));
  12. string substitue = String.Format("[a title="{0}"]{1}[/a]", word, abbr);
  13. string output = Regex.Replace(input, pattern, substitue);
  14.  
  15. b(?:{abbr_1}|{abbr_2}|{abbr_3}|{abbr_n})b
  16.  
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text.RegularExpressions;
  21.  
  22. namespace ConsoleApplication1
  23. {
  24. class Program
  25. {
  26. static void Main(string[] args)
  27. {
  28. var input = @"This is just a little test of the memb to see if it gets picked up.
  29. Deb of course should also be caught here.";
  30. var dictionary = new Dictionary<string,string>
  31. {
  32. {"memb", "Member"}
  33. ,{"deb","Debut"}
  34. };
  35. var regex = "(" + String.Join(")|(", dictionary.Keys.ToArray()) + ")";
  36. foreach (Match metamatch in Regex.Matches(input
  37. , regex /*@"(memb)|(deb)"*/
  38. , RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture))
  39. {
  40. input = input.Replace(metamatch.Value, dictionary[metamatch.Value.ToLower()]);
  41. }
  42. Console.Write (input);
  43. Console.ReadLine();
  44. }
  45. }
  46. }
  47.  
  48. var abbrsWithPipes = "(abbr1|abbr2)";
  49. var regex = new Regex(abbrsWithPipes);
  50. return regex.Replace(html, m => GetReplaceForAbbr(m.Value));
  51.  
  52. public partial class Abbreviations : System.Web.UI.UserControl
  53. {
  54. private Dictionary<String, String> dictionary = DataHelper.GetAbbreviations();
  55.  
  56. protected void Page_Load(object sender, EventArgs e)
  57. {
  58. 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!";
  59.  
  60. var regex = "\b(?:" + String.Join("|", dictionary.Keys.ToArray()) + ")\b";
  61.  
  62. MatchEvaluator myEvaluator = new MatchEvaluator(GetExplanationMarkup);
  63.  
  64. input = Regex.Replace(input, regex, myEvaluator, RegexOptions.IgnoreCase);
  65.  
  66. litContent.Text = input;
  67. }
  68.  
  69. private string GetExplanationMarkup(Match m)
  70. {
  71. return string.Format("<b title='{0}'>{1}</b>", dictionary[m.Value.ToLower()], m.Value);
  72. }
  73. }
  74.  
  75. 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>!
  76.  
  77. public static string GetGlossaryString(string str)
  78. {
  79. 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
  80.  
  81. str = string.Format(" {0} ", str);//quick and dirty way to also search the first and last word in the content.
  82.  
  83. foreach (string word in glossaryWords)
  84. str = Regex.Replace(str, "([\W])(" + word + ")([\W])", "$1<span class='glossaryItem'>$2</span>$3", RegexOptions.IgnoreCase);
  85.  
  86. return str.Trim();
  87. }
Add Comment
Please, Sign In to add comment