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/0
This 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.

Ivonne AspilcuetaPosted Jun 7, 2024, 5:44 PM
@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:
Naimish MakwanaPosted Jun 7, 2024, 4:50 AM
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:
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:Here’s how your updated code would look like:
Thanks
Prasad RaveendranPosted Jun 7, 2024, 1:29 AM
please try with this
Explanation:_\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.