Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the C# Regex equivalent to Java's appendReplacement and appendTail
    primarykey
    data
    text
    <p><strong>UPDATE</strong></p> <p>Here is what I came up with. I haven't tested it yet because it is part of a much larger piece of code that still needs to be ported.</p> <p>Can you see anything that looks out of place?</p> <pre><code>private const string tempUserBlock = "%%%COMPRESS~USER{0}~{1}%%%"; string html = "some html"; int p = 0; var userBlock = new ArrayList(); MatchCollection matcher = preservePatterns[p].Matches(html); int index = 0; StringBuilder sb = new StringBuilder(); int lastValue = 0; foreach(Match match in matcher){ string matchValue = match.Groups[0].Value; if(matchValue.Trim().Length &gt; 0) { userBlock.Add(matchValue); int curIndex = lastValue + match.Index; sb.Append(html.Substring(lastValue, curIndex)); sb.AppendFormat(tempUserBlock, p, index++); lastValue = curIndex + match.Length; } } sb.Append(html.Substring(lastValue)); html = sb.ToString(); </code></pre> <hr> <p><strong>ORIGINAL POST BELOW:</strong></p> <p>Here is the original Java:</p> <pre><code>private static final String tempUserBlock = "%%%COMPRESS~USER{0}~{1}%%%"; String html = "some html"; int p = 0; List&lt;String&gt; userBlock = new ArrayList&lt;String&gt;(); Matcher matcher = patternToMatch.matcher(html); int index = 0; StringBuffer sb = new StringBuffer(); while (matcher.find()) { if (matcher.group(0).trim().length() &gt; 0) { userBlock.add(matcher.group(0)); matcher.appendReplacement(sb, MessageFormat.format(tempUserBlock, p, index++)); } } matcher.appendTail(sb); html = sb.toString(); </code></pre> <p>And my C# conversion so far</p> <pre><code>private const string tempUserBlock = "%%%COMPRESS~USER{0}~{1}%%%"; string html = "some html"; int p = 0; var userBlock = new ArrayList(); MatchCollection matcher = preservePattern.Matches(html); int index = 0; StringBuilder sb = new StringBuilder(); for(var i = 0; i &lt; matcher.Count; ++i){ string match = matcher[i].Groups[0].Value; if(match.Trim().Length &gt; 0) { userBlock.Add(match); // WHAT DO I DO HERE? sb.Append( string.Format(tempUserBlock, p, index++) ); } } // WHAT DO I DO HERE? matcher.appendTail(sb); html = sb.toString(); </code></pre> <p><strong>See comment above, where I ask, "WHAT DO I DO HERE?"</strong></p> <p><em><strong>Clarification</em></strong><br> The Java code above is performing string replacement on some HTML. It saves the originally replaced text because it needs to be re-inserted later after some whitespace compression is completed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload