Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My program first breaks the text into batches.</p> <p>I think you'll agree that resequencing the detail number is the trickiest part. You can do it with a MatchEvaluator delegate.</p> <pre><code>Regex.Replace ( text, // the text replace part of @"(?&lt;=^DETAIL)\d+", // the regex pattern to find. m =&gt; (detailNum++).ToString ("000"), // replacement (evaluated for each match) RegexOptions.Multiline); </code></pre> <p>See how the preceeding code increments <code>detailNum</code> at the begining of each batch.</p> <pre><code> var contents = @"FILE001DETAILCOUNT002 BATCH01 DETAIL001FOO BATCH02 DETAIL001BAR"; // foreach batch.... foreach (Match match in Regex.Matches (contents, @"BATCH\d+\s+(?:(?!BATCH\d+).*\s*)+")) { Console.WriteLine ("==============\r\nFile\r\n================"); int batchNum = 1; int detailNum = 1; StringBuilder temp = new StringBuilder (); TextWriter file = new StringWriter (temp); // Your file here instead of my stringBuilder/StringWriter string batchText = match.Value; int count = Regex.Matches (batchText, @"^DETAIL\d+", RegexOptions.Multiline).Count; file.WriteLine ("FILE001DETAILCOUNT{0:000}", count); string newText = Regex.Replace (batchText, @"(?&lt;=^BATCH)\d+", batchNum.ToString ("000"), RegexOptions.Multiline); newText = Regex.Replace ( newText, @"(?&lt;=^DETAIL)\d+", m =&gt; (detailNum++).ToString ("000"), // replacement (evaluated for each match) RegexOptions.Multiline); file.Write (newText); Console.WriteLine (temp.ToString ()); } </code></pre> <p>prints</p> <pre><code>============== File ================ FILE001DETAILCOUNT001 BATCH001 DETAIL001FOO ============== File ================ FILE001DETAILCOUNT001 BATCH001 DETAIL001BAR </code></pre>
 

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