code_junkie

Regular expression to convert mark down to HTML

Nov 11th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. This would be *italicized* text and this would be **bold** text
  2.  
  3. This would be <em>italicized</em> text and this would be <strong>bold</strong> text
  4.  
  5. private string DoItalicsAndBold (string text)
  6. {
  7. // <strong> must go first:
  8. text = Regex.Replace (text, @"(**|__) (?=S) (.+?[*_]*) (?<=S) 1",
  9. new MatchEvaluator (BoldEvaluator),
  10. RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
  11.  
  12. // Then <em>:
  13. text = Regex.Replace (text, @"(*|_) (?=S) (.+?) (?<=S) 1",
  14. new MatchEvaluator (ItalicsEvaluator),
  15. RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
  16. return text;
  17. }
  18.  
  19. private string ItalicsEvaluator (Match match)
  20. {
  21. return string.Format ("<em>{0}</em>", match.Groups[2].Value);
  22. }
  23.  
  24. private string BoldEvaluator (Match match)
  25. {
  26. return string.Format ("<strong>{0}</strong>", match.Groups[2].Value);
  27. }
Add Comment
Please, Sign In to add comment