Advanced Text Replacement with Regular Expressions in C#: A Practical Guide from Data Formatting to CSV Conversion

Nov 30, 2025 · Programming · 12 views · 7.8

Keywords: C# | Regular Expressions | Text Replacement | CSV Conversion | Data Formatting

Abstract: This article provides an in-depth exploration of Regex.Replace method applications in C# for data formatting scenarios. Through a concrete CSV conversion case study, it analyzes regular expression pattern design, capture group usage, and replacement strategies. Combining Q&A data and official documentation, the article offers complete code implementations and performance optimization recommendations to help developers master regular expression solutions for complex text processing.

Introduction and Problem Context

In modern software development, text data formatting is a common requirement. Particularly in data export and system integration scenarios, converting structured data to standard formats like CSV (Comma-Separated Values) files is crucial. This article explores how to implement complex data format conversions using C# regular expressions, based on a real-world development case.

Overview of Regular Expression Replacement Methods

C#'s Regex.Replace method provides powerful text replacement capabilities. According to reference documentation, this method has multiple overloaded versions supporting different matching options and timeout settings. The core functionality replaces all substrings matching a regular expression pattern with a specified replacement string in a given input string.

Basic syntax structure:

public static string Replace(string input, string pattern, string replacement);
public static string Replace(string input, string pattern, string replacement, RegexOptions options);
public static string Replace(string input, string pattern, MatchEvaluator evaluator);

Case Study: Employee Data CSV Conversion

Original data format contains employee name, salary, and position information:

FirstName LastName Salary    Position
-------------------------------------
John      Smith    $100,000.00  M

Target conversion to CSV format:

John Smith,100000,M

Solution Implementation

Based on the best answer solution, we adopt a two-step replacement strategy:

Step 1: Basic Format Processing

sb_trim = Regex.Replace(stw, @"\s+\$|\s+(?=\w+$)", ",");

This step's regular expression @"\s+\$|\s+(?=\w+$)" contains two main components:

Step 2: Numerical Refinement Processing

sb_trim = Regex.Replace(sb_trim, @"(?<=\d),(?=\d)|[\.]0+(?=,)", "");

This step handles numerical formatting:

Complete Code Implementation

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

public class CSVFormatter
{
    public void FormatToCSV(string filepath, System.Windows.Forms.ListBox listBox1)
    {
        using (var fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write))
        {
            using (var sw = new StreamWriter(fs))
            {
                foreach (string stw in listBox1.Items)
                {
                    string sb_trim = Regex.Replace(stw, @"\s+\$|\s+(?=\w+$)", ",");
                    sb_trim = Regex.Replace(sb_trim, @"(?<=\d),(?=\d)|[\.]0+(?=,)", "");
                    sw.WriteLine(sb_trim);
                }
            }
        }
    }
}

In-depth Regular Expression Analysis

Character Classes and Quantifiers

The solution utilizes various regular expression constructs:

Application of Lookaround Assertions

Positive lookahead (?=...) and lookbehind (?<=...) play crucial roles in the solution:

Performance Optimization and Best Practices

Compiling Regular Expressions

For frequently used patterns, compilation is recommended:

Regex regex = new Regex(@"\s+\$|\s+(?=\w+$)", RegexOptions.Compiled);

Timeout Handling

In production environments, reasonable timeout settings should be implemented:

try
{
    string result = Regex.Replace(input, pattern, replacement, 
        RegexOptions.None, TimeSpan.FromSeconds(2));
}
catch (RegexMatchTimeoutException)
{
    // Handle timeout situations
}

Alternative Solution Comparison

While Answer 2 provides an alternative using MatchEvaluator, the best answer's two-step replacement approach offers advantages in readability and performance:

Application Scenario Extensions

The techniques discussed in this article can be applied to:

Conclusion

Through detailed analysis in this article, we have demonstrated how to efficiently solve complex data formatting problems using C# regular expressions. Key points include: designing appropriate regular expression patterns, fully utilizing lookaround assertions, and considering performance optimization measures. These techniques are applicable not only to CSV conversion scenarios but also to various other text processing requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.