static public string GetRxPattern(RxPattern rxPattern, bool?soloMatch) { switch (rxPattern) { case RxPattern.Email: string email = @"\w+([-.+']\w+)*@\w+([.-]\w+)*\.\w{2,6}"; if (soloMatch == null) { return(email); } if ((bool)soloMatch) { return(string.Format(@"^{0}$", email)); } else { return(string.Format(@"\b{0}\b", email)); } case RxPattern.PhoneUS: string phone = @"((\(\d{3}\)[\s]?)|(\d{3}[\s-]))\d{3}[\s-]\d{4}"; if (soloMatch == null) { return(phone); } if ((bool)soloMatch) { return(string.Format(@"^{0}$", phone)); } else { return(string.Format(@"(?<=[^0-9a-zA-Z]|^){0}\b", phone)); // notes } case RxPattern.SocSecNumUS: string socsecnum = @"\d{3}-\d{2}-\d{4}"; if (soloMatch == null) { return(socsecnum); } if ((bool)soloMatch) { return(string.Format(@"^{0}$", socsecnum)); } else { return(string.Format(@"\b{0}\b", socsecnum)); } case RxPattern.WebURL: string url = @"(http(s)?://)([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; if (soloMatch == null) { return(url); } if ((bool)soloMatch) { return(string.Format(@"^{0}$", url)); } else { return(string.Format(@"\b{0}\b", url)); } case RxPattern.ZIPCodeUS: string zip = @"\d{5}(-\d{4})?"; if (soloMatch == null) { return(zip); } if ((bool)soloMatch) { return(string.Format(@"^{0}$", zip)); } else { return(string.Format(@"\b{0}\b", zip)); } default: throw new ArgumentException(); // for phone, we need this beast: (?<=[^0-9a-zA-Z]|^), a look ahead, because if there is a // parentheses with the area code, the \b seems to go awol and the first paren // does not become part of the match. This *may* be an error in the .NET Regex // engine's handling of \b. That's a highly complex area though (e.g. note that specified // word breaks (\b) do *not* become part of matches, opposite almost everything else) } }
static public string GetRxPattern(RxPattern rxPattern) { return(GetRxPattern(rxPattern, null)); }
public static Match RxMatch(this string s, RxPattern rxPatternType) { return(Regex.Match(s, XRegex.GetRxPattern(rxPatternType, false))); }
public static MatchCollection RxMatches(this string s, RxPattern rxPatternType) { return(Regex.Matches(s, XRegex.GetRxPattern(rxPatternType, false))); }
public static bool RxIsMatch(this string s, RxPattern rxPatternType) { return(Regex.IsMatch(s, XRegex.GetRxPattern(rxPatternType, true))); }