1

I deleted my previous post in hopes this one is better and clear to be able to get help. I appreciate it.

So far, with my code I am uploading a CSV file to format it as: I have skip the headers and also instead of comma separator , added a pipeline separator | and delete the following value: ".\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001" in all lines/rows when creates the new CSV File.

The problem that I am not able to resolve is when uploading a new CSV File, the value I need to delete: ".\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001" . The last number: _0001" increments by one on every CSV file I upload to formatted.

I need help on how to delete the whole string value including the increment of last value:

".\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001" . The last number: _0001"

".\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001" . The last number: _0002"

".\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001" . The last number: _0003"

".\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001" . The last number: _0004"

This is my code:

Thank you

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (String.IsNullOrEmpty(FromFile))
            { 
                lblStatus.Text = "Missing CSV Information";
                Application.DoEvents();
                MessageBox.Show("Please select CSV file first.");
                return;
            }

            if (String.IsNullOrEmpty(ToFile))
            {
                lblStatus.Text = "Missing Save Information";
                Application.DoEvents();
                MessageBox.Show("Please enter save information.");
                return;
            }
            else if (File.Exists(ToFile))
            {
                // delete old file
                File.Delete(ToFile);
            }

            btnProcess.Enabled = false;
            lblStatus.Text = "Processing...";
            Application.DoEvents();

            var lines = File.ReadAllLines(FromFile).Skip(1);
            string docId = "";
            string[] oldLine = null;

            foreach (var line in lines)
            {
                var newLine = line.Replace("\"", "").Replace(".\\", "").Replace("\\", "").Replace("MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001", "").Split(',');

                if (docId != newLine[0])
                {
                    docId = newLine[0];
                    oldLine = newLine;
                } 
                else
                {
                    for (int i = COPY_FROM - 1; i < newLine.Length; i++)
                        newLine[i] = oldLine[i];
                }

                using (StreamWriter sr = new StreamWriter(ToFile, true))
                {
                    sr.WriteLine(string.Join("|", newLine));
                }
            }

            btnProcess.Enabled = true;
            lblStatus.Text = "Completed";
            Application.DoEvents();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error! Ex: " + ex.Message);
        }
    }`
2
  • In short, you want to replace MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001 and all follow-up numbers to nothing? Commented May 24 at 21:38
  • @JeroenvanLangen that is correct, but I do not know how to make a dynamic code to accomplish that. Commented May 24 at 21:39

1 Answer 1

2

I would, in this case, use a regex, because it should match multiple digit combinations. The trick is that instead of a 100% match, it can match with wildcards. It's really powerful, but it's not easy.

Something like:

// Regex pattern. As you can see the zero's are replace by "\d{8}"
// which means, 8 times any digit (0-9)
string pattern = @"MEMBERRECORDS_SIGNATURECARD_\d{8}_\d{8}_\d{4}";

// Create a compiled regex object
Regex regex = new Regex(pattern, RegexOptions.Compiled);

// for each line...
foreach(var line in file)
{
    // first remove the slashes.
    var removedSpecialChars = line.Replace("\"", "").Replace(".\\", "").Replace("\\", "");

    // call the regex.Replace and it should replace a match with empty string.
    var newLine = regex.Replace(removedSpecialChars, "").Split(',');



    // rest of code...
}

You should lookup for it .NET regular expressions. Very valuable about what it is and what you can do with it.


Removing the . " \ can also be done with regex, but needs a separate regex.

For example:

// Regex pattern. As you can see the zero's are replace by "\d{8}"
// which means, 8 times any digit (0-9)
string pattern = @"MEMBERRECORDS_SIGNATURECARD_\d{8}_\d{8}_\d{4}";

// Create a compiled regex object
Regex regex = new Regex(pattern, RegexOptions.Compiled);

// Define special characters, 
string specialCharsPattern = @"[""\\.]";

// Create a compiled regex object
Regex specialCharsRegex = new Regex(specialCharsPattern, RegexOptions.Compiled);

// For each line...
foreach(var line in file)
{
    // Replace the matched special characters with an empty string
    var removedSpecialChars = specialCharsRegex.Replace(line, string.Empty);

    // call the regex.Replace and it should replace a match with empty string.
    var newLine = regex.Replace(removedSpecialChars, "").Split(',');


    // rest of code...
}
1
  • 1
    Thank you so much for teaching me something new, I thought about REGEX, but I did not know how to. I appreciate it. Commented May 25 at 0:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.