public static SqlChars RegExReplace(SqlString sourceString, SqlString pattern, SqlString replacement, int matchId, SqlString options) { RegexOptions regOptions = RegexOptions.None; RegexOptionsEnum.TyPrase(options.Value, out regOptions); regOptions |= RegexOptions.Compiled; Match m = null; Regex r = new Regex(pattern.Value, regOptions); if (matchId == 0) { return(new SqlChars(r.Replace(sourceString.Value, replacement.Value))); } if (matchId == 1) { m = r.Match(sourceString.Value); } else if (matchId > 1) { MatchCollection mc = r.Matches(sourceString.Value); m = mc != null && mc.Count > matchId - 1 ? mc[matchId - 1] : null; } return(m != null ? new SqlChars(m.Result(replacement.Value)) : SqlChars.Null); }
public static IEnumerable RegExMatches(SqlString sourceString, SqlString pattern, SqlString options) { RegexOptions regOptions = RegexOptions.None; RegexOptionsEnum.TyPrase(options.Value, out regOptions); regOptions |= RegexOptions.Compiled; Regex r = new Regex(pattern.Value, regOptions); int rowId = 0; int matchId = 0; string[] grpNames = r.GetGroupNames(); foreach (Match m in r.Matches(sourceString.Value)) { matchId++; for (int i = 0; i < m.Groups.Count; i++) { string grpName = grpNames[i]; var grp = m.Groups[i]; for (int j = 0; j < grp.Captures.Count; j++) { var cap = grp.Captures[j]; yield return(new RegExRow(++rowId, matchId, i, grpName, grp.Success, j, new SqlChars(cap.Value))); } } } }
public static SqlChars RegExMatchName(SqlString sourceString, SqlString pattern, int matchId, SqlString groupname, SqlString options) { RegexOptions regOptions = RegexOptions.None; RegexOptionsEnum.TyPrase(options.Value, out regOptions); regOptions |= RegexOptions.Compiled; Match m = null; Regex r = new Regex(pattern.Value, regOptions); if (matchId == 1) { m = r.Match(sourceString.Value); } else if (matchId > 1) { MatchCollection mc = r.Matches(sourceString.Value); m = mc != null && mc.Count > matchId - 1 ? mc[matchId - 1] : null; } if (groupname.IsNull || string.IsNullOrEmpty(groupname.Value)) { return(new SqlChars(m.Value)); } else { return(m != null ? new SqlChars(m.Groups[groupname.Value].Value) : SqlChars.Null); } }
public static SqlBoolean RegExIsMatch(SqlString sourceString, SqlString pattern, SqlString options) { RegexOptions regOptions = RegexOptions.None; RegexOptionsEnum.TyPrase(options.Value, out regOptions); regOptions |= RegexOptions.Compiled; Regex r = new Regex(pattern.Value, regOptions); return(new SqlBoolean(r.IsMatch(sourceString.Value))); }
public static IEnumerable RegExMatchesReplace(SqlString sourceString, SqlString pattern, SqlString replacement, SqlString options) { RegexOptions regOptions = RegexOptions.None; RegexOptionsEnum.TyPrase(options.Value, out regOptions); regOptions |= RegexOptions.Compiled; Regex r = new Regex(pattern.Value, regOptions); int matchId = 0; foreach (Match m in r.Matches(sourceString.Value)) { yield return(new RegExRowReplace(++matchId, new SqlChars(m.Value), new SqlChars(m.Result(replacement.Value)))); } }