internal static string Reformat (Regex regex, string replacement) { replacement = JavaUtils.ReplaceAll (replacement, BACKSLASH_PATTERN, JAVA_BACKSLASH); replacement = JavaUtils.ReplaceAll (replacement, DOUBLE_DOLLAR_PATTERN, JAVA_DOLLAR); Pattern p = Pattern.compile (SINGLE_DOLLAR_PATTERN); Matcher m = p.matcher ((CharSequence) (object) replacement); StringBuffer sb = new StringBuffer (); while (m.find ()) { if (m.start (1) >= 0) { int groupNumber = regex.GroupNumberFromName (m.group (1)); if (groupNumber >= 0) { m.appendReplacement (sb, @"\$" + regex.GetJavaNumberByNetNumber (groupNumber)); continue; } if (int.TryParse (m.group (1), out groupNumber) && groupNumber <= regex.GroupCount) { m.appendReplacement (sb, @"\$" + regex.GetJavaNumberByNetNumber (groupNumber)); continue; } m.appendReplacement (sb, JAVA_DOLLAR + "{" + m.group (1) + "}"); continue; } if (m.start (2) >= 0) { int netGroupNumber = int.Parse (m.group (2)); if (netGroupNumber > regex.GroupCount) { m.appendReplacement (sb, JAVA_DOLLAR + netGroupNumber); continue; } m.appendReplacement (sb, @"\$" + regex.GetJavaNumberByNetNumber (netGroupNumber)); continue; } m.appendReplacement (sb, JAVA_DOLLAR); } m.appendTail (sb); return sb.ToString (); }
public static string ReplaceString(string whom, string what, string with) { int j = -1; int i = whom.IndexOf(what); if (i == -1) return whom; StringBuffer b = new StringBuffer(); b.ensureCapacity(whom.Length); while (i > -1) { b.append( whom.Substring(j + what.Length, i - j - what.Length) + with ); j = i; i = whom.IndexOf(what, i + what.Length); } b.append( whom.Substring(j + what.Length) ); return b.ToString(); }
public override string Replace (Regex regex, string input, string replacement, int count, int startat) { if (regex.SameGroupNamesFlag) { return base.Replace (regex, input, replacement, count, startat); } if (count < 0) { count = Int32.MaxValue; } string replacementPattern = ReplacementData.Reformat (regex, replacement); Matcher m = JavaPattern.matcher ((CharSequence) (object) input); StringBuffer sb = new StringBuffer (); if (count > 0 && m.find (startat)) { ReplacementData.ReplaceMatch (replacementPattern, m, sb, input, _patternData); } for (int matchesCounter = 1; matchesCounter < count; ++matchesCounter) { if (!m.find ()) { break; } ReplacementData.ReplaceMatch (replacementPattern, m, sb, input, _patternData); } m.appendTail (sb); return sb.ToString (); }
internal static string ReplaceWithLookBehind (string input, string pattern, string lookBehindPattern, string replacement) { Pattern p = Pattern.compile (pattern); Pattern behindPattern = Pattern.compile (lookBehindPattern); Matcher m = p.matcher ((CharSequence) (object) input); StringBuffer sb = new StringBuffer (); while (m.find ()) { Matcher pm = behindPattern.matcher ((CharSequence) (object) input.Substring (0, m.start())); if(pm.find()) m.appendReplacement(sb, replacement); } m.appendTail (sb); return sb.ToString(); }