public bool filter(Regsub rs, System.Text.StringBuilder sb) { sunlabs.brazil.util.regexp.Regexp.applySubspec(rs, subspec, sb); return all; }
/// <summary> Utility method to give access to the standard substitution algorithm /// used by <code>sub</code> and <code>subAll</code>. Appends to the /// string buffer the string generated by applying the substitution /// parameter to the matched region. /// /// </summary> /// <param name="">rs /// Information about the matched region. /// /// </param> /// <param name="">subspec /// The substitution parameter. /// /// </param> /// <param name="">sb /// StringBuffer to which the generated string is appended. /// </param> public static void applySubspec(Regsub rs, string subspec, System.Text.StringBuilder sb) { try { int len = subspec.Length; for (int i = 0; i < len; i++) { char ch = subspec[i]; switch (ch) { case '&': { sb.Append(rs.matched()); break; } case '\\': { i++; ch = subspec[i]; if ((ch >= '0') && (ch <= '9')) { string match = rs.submatch(ch - '0'); if ((System.Object) match != null) { sb.Append(match); } break; } // fall through. } goto default; default: { sb.Append(ch); } break; } } } catch (System.IndexOutOfRangeException e) { /* * Ignore malformed substitution pattern. * Return string matched so far. */ } }
public string sub(string str, Filter rf) { Regsub rs = new Regsub(this, str); if (rs.nextMatch() == false) { return str; } System.Text.StringBuilder sb = new System.Text.StringBuilder(); do { sb.Append(rs.skipped()); if (rf.filter(rs, sb) == false) { break; } } while (rs.nextMatch()); sb.Append(rs.rest()); return sb.ToString(); }
/// <summary> Matches a string against a regular expression and replaces the first /// match with the string generated from the substitution parameter. /// /// </summary> /// <param name="">str /// The string to match against this regular expression. /// /// </param> /// <param name="">subspec /// The substitution parameter, described in <a href=#regsub> /// REGULAR EXPRESSION SUBSTITUTION</a>. /// /// </param> /// <returns> The string formed by replacing the first match in /// <code>str</code> with the string generated from /// <code>subspec</code>. If no matches were found, then /// the return value is <code>null</code>. /// </returns> public string sub(string str, string subspec) { Regsub rs = new Regsub(this, str); if (rs.nextMatch()) { System.Text.StringBuilder sb = new System.Text.StringBuilder(rs.skipped()); applySubspec(rs, subspec, sb); sb.Append(rs.rest()); return sb.ToString(); } else { return null; } }