Advertisement
kyrathasoft

MyRegex.cs

Jun 13th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace com.williambryanmiller.regularexpressions {
  6.  
  7. class MyRegex {
  8.  
  9. public const string MatchEmailPattern =
  10. @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
  11.  
  12. //matches extraneous whitespace (i.e., 2 or more consecutive spaces but
  13. //does NOT consider a carriage return or line feed as whitespace
  14. public const string MatchExtraWhitespace = @"[^\S\r\n]{2,}";
  15.  
  16. public const string MatchLonelyPeriods = @"(\s{1,}\.)";
  17.  
  18. public const string Match_to_ot_Transposition = @"(\bot\b)";
  19.  
  20. public const string MatchPeriodsWithoutTrailingSpace = @"\.(?! |$)";
  21.  
  22.  
  23.  
  24.  
  25. public static string fixLonelyPeriods(string p) {
  26. //find instances of one or more space characters followed by a period,
  27. //and replace with a period
  28. return Regex.Replace(p, MatchLonelyPeriods, ".");
  29. }
  30.  
  31. public static string fixPeriodsWithoutTrailingSpace(string p) {
  32. return Regex.Replace(p, MatchPeriodsWithoutTrailingSpace, ". ");
  33. }
  34.  
  35. public static string fixTransposition(string p, string pattern, string replacement) {
  36. return Regex.Replace(p, pattern, replacement);
  37. }
  38.  
  39. public static bool isEmail(string sEmail) {
  40. if (sEmail != null) {
  41. return Regex.IsMatch(sEmail, MatchEmailPattern);
  42. } else {
  43. //if sEmail is null, we don't test at all
  44. return false;
  45. }
  46. }
  47.  
  48. public static string removeExtraWhitespace(string p) {
  49. return Regex.Replace(p, MatchExtraWhitespace, " ");
  50. }
  51.  
  52. public static int numCarriageReturnLineFeeds(string text) {
  53. Regex RE = new Regex("\r\n", RegexOptions.Multiline);
  54. MatchCollection matches = RE.Matches(text);
  55. return matches.Count;;
  56. }
  57.  
  58. public static int numOfMatches(string text, string expr) {
  59. MatchCollection mc = Regex.Matches(text, expr);
  60. return mc.Count;
  61. }
  62.  
  63. public static int numWordsEndingIn(string text, string ending){
  64. MatchCollection mc = Regex.Matches(text, @"\b\S*" + ending + @"\b");
  65. return mc.Count;
  66. }
  67.  
  68.  
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement