Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace com.williambryanmiller.regularexpressions {
- class MyRegex {
- public const string MatchEmailPattern =
- @"^[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]$";
- //matches extraneous whitespace (i.e., 2 or more consecutive spaces but
- //does NOT consider a carriage return or line feed as whitespace
- public const string MatchExtraWhitespace = @"[^\S\r\n]{2,}";
- public const string MatchLonelyPeriods = @"(\s{1,}\.)";
- public const string Match_to_ot_Transposition = @"(\bot\b)";
- public const string MatchPeriodsWithoutTrailingSpace = @"\.(?! |$)";
- public static string fixLonelyPeriods(string p) {
- //find instances of one or more space characters followed by a period,
- //and replace with a period
- return Regex.Replace(p, MatchLonelyPeriods, ".");
- }
- public static string fixPeriodsWithoutTrailingSpace(string p) {
- return Regex.Replace(p, MatchPeriodsWithoutTrailingSpace, ". ");
- }
- public static string fixTransposition(string p, string pattern, string replacement) {
- return Regex.Replace(p, pattern, replacement);
- }
- public static bool isEmail(string sEmail) {
- if (sEmail != null) {
- return Regex.IsMatch(sEmail, MatchEmailPattern);
- } else {
- //if sEmail is null, we don't test at all
- return false;
- }
- }
- public static string removeExtraWhitespace(string p) {
- return Regex.Replace(p, MatchExtraWhitespace, " ");
- }
- public static int numCarriageReturnLineFeeds(string text) {
- Regex RE = new Regex("\r\n", RegexOptions.Multiline);
- MatchCollection matches = RE.Matches(text);
- return matches.Count;;
- }
- public static int numOfMatches(string text, string expr) {
- MatchCollection mc = Regex.Matches(text, expr);
- return mc.Count;
- }
- public static int numWordsEndingIn(string text, string ending){
- MatchCollection mc = Regex.Matches(text, @"\b\S*" + ending + @"\b");
- return mc.Count;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement