Hello,
I need help on how to delete a dynamic string within a path using Regex:
".\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001\" . The last number: _0001\"
".\LOANS_ADVERSEACTION_00000000_00000000_0001\" . The last number: _0002\"
".\MEMBERRECORDS_IRADOCUMENTS_00000000_00000000_0001\" . The last number: _0003\"
So far I have use many similar Regex to this: ^[^_]*_[^_]* But definitely I am ot applying it correectly, it is not working.
Also in the 3rd column where the value is tif or pdf change it for a numeric value, per example: tif = 2, pdf = 16
143|LOANSADVERSEACTION|tif|1|.\LOANS_ADVERSEACTION_00000000_00000000_0001\613742_001.tif||713797998|
143|LOANSADVERSEACTION|tif|1|.\LOANS_ADVERSEACTION_00000000_00000000_0001\613749_001.tif||3633|||11/143|LOANSADVERSEACTION|PDF|1|.\LOANS_ADVERSEACTION_00000000_00000000_0001\613751_001.tif||713799426|143|LOANSADVERSEACTION|tif|1|.\LOANS_ADVERSEACTION_00000000_00000000_0001\613755_001.tif||5229||07/0This is my code:
private void button2_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(FromFile))
{
lblStatus.Text = "Missing CSV Information";
MessageBox.Show("Please select a CSV file first.");
return;
}
if (string.IsNullOrEmpty(ToFile))
{
lblStatus.Text = "Missing Save Information";
MessageBox.Show("Please enter save information.");
return;
}
else if (File.Exists(ToFile))
{
File.Delete(ToFile);
}
btnProcess.Enabled = false;
lblStatus.Text = "Processing...";
//TIF = 2, PDF= 16 column 6
var lines = File.ReadAllLines(FromFile).Skip(1);
string docId = "";
string[] oldLine = null;
string pattern = @"\.\\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_\d{4}\\|\.\\LOANS_ADVERSEACTION_00000000_00000000_\d{4}\\|\.\\MEMBERRECORDS_IRADOCUMENTS_00000000_00000000_\d{4}\\";
//string pattern = @"\.\\^[^_]*_[^_]*_00000000_00000000_\d{4}\\";
var sb = new StringBuilder();
foreach (var line in lines)
{
var newLine = Regex.Replace(line, pattern, "").Replace("\"", "").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];
}
sb.AppendLine(string.Join("|", newLine));
}
File.WriteAllText(ToFile, sb.ToString());
//btnProcess.Enabled = true;
//lblStatus.Text = "Completed";
}Any help will be helpful and appreciate it. Thank you.


Naimish MakwanaRank #324 · 7.7K reputationPosted Jun 7, 2024, 12:50 AM EDT
Sure, I can help you with that.
Firstly, the regular expression you’re using seems to be incorrect. If you want to match the dynamic string in the path and replace it, you can use the following pattern:
string pattern = @"\\.\\[^_]*_[^_]*_[^_]*_[^_]*_\d{4}\\";This pattern will match any string that starts with
.\, followed by any characters until it encounters an underscore_, and repeats this four times, and finally matches four digits\d{4}and a backslash\\.Secondly, for replacing ‘tif’ with ‘2’ and ‘pdf’ with ‘16’ in the third column, you can add the following code inside your
foreachloop:if (newLine[2].ToLower() == "tif") newLine[2] = "2"; else if (newLine[2].ToLower() == "pdf") newLine[2] = "16";Here’s how your updated code would look like:
private void button2_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(FromFile)) { lblStatus.Text = "Missing CSV Information"; MessageBox.Show("Please select a CSV file first."); return; } if (string.IsNullOrEmpty(ToFile)) { lblStatus.Text = "Missing Save Information"; MessageBox.Show("Please enter save information."); return; } else if (File.Exists(ToFile)) { File.Delete(ToFile); } btnProcess.Enabled = false; lblStatus.Text = "Processing..."; //TIF = 2, PDF= 16 column 6 var lines = File.ReadAllLines(FromFile).Skip(1); string docId = ""; string[] oldLine = null; string pattern = @"\\.\\[^_]*_[^_]*_[^_]*_[^_]*_\d{4}\\"; var sb = new StringBuilder(); foreach (var line in lines) { var newLine = Regex.Replace(line, pattern, "").Replace("\"", "").Split(','); if (newLine[2].ToLower() == "tif") newLine[2] = "2"; else if (newLine[2].ToLower() == "pdf") newLine[2] = "16"; if (docId != newLine[0]) { docId = newLine[0]; oldLine = newLine; } else { for (int i = COPY_FROM - 1; i < newLine.Length; i++) newLine[i] = oldLine[i]; } sb.AppendLine(string.Join("|", newLine)); } File.WriteAllText(ToFile, sb.ToString()); //btnProcess.Enabled = true; //lblStatus.Text = "Completed"; } catch (Exception ex) { // Handle exception } }Thanks
Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 6, 2024, 9:29 PM EDT
please try with this
private void button2_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(FromFile)) { lblStatus.Text = "Missing CSV Information"; MessageBox.Show("Please select a CSV file first."); return; } if (string.IsNullOrEmpty(ToFile)) { lblStatus.Text = "Missing Save Information"; MessageBox.Show("Please enter save information."); return; } else if (File.Exists(ToFile)) { File.Delete(ToFile); } btnProcess.Enabled = false; lblStatus.Text = "Processing..."; // TIF = 2, PDF = 16 column 6 var lines = File.ReadAllLines(FromFile).Skip(1); string docId = ""; string[] oldLine = null; // Regular expression pattern to remove the dynamic part of the path string pattern = @"_\d{4}\\"; var sb = new StringBuilder(); foreach (var line in lines) { // Remove the dynamic part of the path var newLine = Regex.Replace(line, pattern, "").Replace("\"", "").Split(','); // Replace file extension in column 6 with numeric value if (newLine.Length > 5) { if (newLine[5].Equals("tif", StringComparison.OrdinalIgnoreCase)) { newLine[5] = "2"; } else if (newLine[5].Equals("pdf", StringComparison.OrdinalIgnoreCase)) { newLine[5] = "16"; } } if (docId != newLine[0]) { docId = newLine[0]; oldLine = newLine; } else { for (int i = COPY_FROM - 1; i < newLine.Length; i++) newLine[i] = oldLine[i]; } sb.AppendLine(string.Join("|", newLine)); } File.WriteAllText(ToFile, sb.ToString()); //btnProcess.Enabled = true; //lblStatus.Text = "Completed"; } catch (Exception ex) { lblStatus.Text = "Error: " + ex.Message; MessageBox.Show("An error occurred: " + ex.Message); } }_\d{4}\\matches the underscore, followed by exactly four digits, and the backslash.tiforpdfand replace it with2or16respectively.This should fulfill your requirements of removing the dynamic part of the path and replacing the file extensions with their corresponding numeric values. Adjust
COPY_FROMas per your actual column index for copying values from the old line to the new line.Ivonne AspilcuetaRank #4,887 · 320 reputationPosted Jun 7, 2024, 1:44 PM EDT
@Naimish and @ Prasad. Thank you so much for both contribution and for helping me to increase my learning skills, this is how I use it:
private void button2_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(FromFile)) { lblStatus.Text = "Missing CSV Information"; MessageBox.Show("Please select a CSV file first."); return; } if (string.IsNullOrEmpty(ToFile)) { lblStatus.Text = "Missing Save Information"; MessageBox.Show("Please enter save information."); return; } else if (File.Exists(ToFile)) { File.Delete(ToFile); } btnProcess.Enabled = false; lblStatus.Text = "Processing..."; //TIF = 2, PDF= 16 column 6 var lines = File.ReadAllLines(FromFile).Skip(1); string docId = ""; string[] oldLine = null; string pattern = @".\\[^_]*_[^_]*_\d{8}_\d{8}_\d{4}\\"; var sb = new StringBuilder(); foreach (var line in lines) { var newLine = Regex.Replace(line, pattern, "").Replace("\"", "").Split(','); // Replace file extension in column 6 with numeric value if (newLine.Length > 5) { if (newLine[5].Equals("tif", StringComparison.OrdinalIgnoreCase)) { newLine[5] = "2"; } else if (newLine[5].Equals("pdf", StringComparison.OrdinalIgnoreCase)) { newLine[5] = "16"; } } if (docId != newLine[0]) { docId = newLine[0]; oldLine = newLine; } else { for (int i = COPY_FROM - 1; i < newLine.Length; i++) newLine[i] = oldLine[i]; } sb.AppendLine(string.Join("|", newLine)); } File.WriteAllText(ToFile, sb.ToString()); //btnProcess.Enabled = true; //lblStatus.Text = "Completed"; } catch (FileNotFoundException ex) { MessageBox.Show($"File not found: {ex.Message}"); } catch (IOException ex) { MessageBox.Show($"IO error: {ex.Message}"); }