protected static string SubstituteKownValue(string transformValue, RegularExpressions.Regex patternRegex, string patternPrefix, GetValueCallback getValueDelegate ) { int position = 0; List<RegularExpressions.Match> matchsExpr = new List<RegularExpressions.Match>(); do { position = transformValue.IndexOf(patternPrefix, position, StringComparison.OrdinalIgnoreCase); if (position > -1) { RegularExpressions.Match match = patternRegex.Match(transformValue, position); // Add the successful match to collection if (match.Success) { matchsExpr.Add(match); position = match.Index + match.Length; } else { position++; } } } while (position > -1); System.Text.StringBuilder strbuilder = new StringBuilder(transformValue.Length); if (matchsExpr.Count > 0) { strbuilder.Remove(0, strbuilder.Length); position = 0; int index = 0; foreach (RegularExpressions.Match match in matchsExpr) { strbuilder.Append(transformValue.Substring(position, match.Index - position)); RegularExpressions.Capture captureTagName = match.Groups["tagname"]; string attributeName = captureTagName.Value; string newValue = getValueDelegate(attributeName); if (newValue != null) // null indicate that the attribute is not exist { strbuilder.Append(newValue); } else { // keep original value strbuilder.Append(match.Value); } position = match.Index + match.Length; index++; } strbuilder.Append(transformValue.Substring(position)); transformValue = strbuilder.ToString(); } return transformValue; }
/** * Helper method for extracting multiple matches from Tweet text. * * @param pattern to match and use for extraction * @param text of the Tweet to extract from * @param groupNumber the capturing group of the pattern that should be added to the list. * @return list of extracted values, or an empty list if there were none. */ private List<String> ExtractList(TRE.Regex pattern, String text, int groupNumber) { List<String> extracted = new List<String>(); TRE.Match matcher = pattern.Match(text); while (matcher.Success) { extracted.Add(matcher.Groups[groupNumber].Value); matcher = matcher.NextMatch(); } return extracted; }