Reputation: 162
I wish to find a match of *|END_OF_PARAM|ABC_XYZ_123.txt
in a given string (meaning string starts with anything but containing |END_OF_PARAM|
followed by a filename(which has alphabets, numbers, _ ,-) and ending in ".txt").
eg:
string input = "| AB|3|20200914-01|5| | |END_OF_PARAM|ABC-XYZ-20200914-PIA-03_05_20200914132900.txt";
string pattern = @"*/([A-Za-z0-9\-]+)\.txt$" // What exactly should go here?
Match match = Regex.Match(input, @"*/([A-Za-z0-9\-]+)\.txt$",
RegexOptions.IgnoreCase);
if (match.Success)
{
Console.WriteLine("Match!"));
}
Output I need is ABC-XYZ-20200914-PIA-03_05_20200914132900.txt
ps : Somelines end with |END_OF_PARAM|
and don't have the filename after them, such lines should be ignored.
I don't know much of RegEx, tried to learn it and get my task done but it's taking longer than expected. Let me know if any additional data is needed. Thank you.
Upvotes: 0
Views: 86
Reputation: 163632
The character class is missing an underscore to match the filename in total. If you want to include the |END_OF_PARAM|
part you should add it to the match.
To differentiate the filename from the total match, you could capturing it in a group and get that value.
^.*\|END_OF_PARAM\|([A-Za-z0-9_-]+\.txt)$
Explanation
^
Start of string.*
Match any char except a newline 0+ times\|END_OF_PARAM\|
Match END_OF_PARAM
between a pipe at the left and right(
Capture group 1
[A-Za-z0-9_-]+\.txt
Match 1+ times any of the listed chars followed by .txt
)
Capture group 1$
End of stringstring input = "| AB|3|20200914-01|5| | |END_OF_PARAM|ABC-XYZ-20200914-PIA-03_05_20200914132900.txt";
string pattern = @"^.*\|END_OF_PARAM\|([A-Za-z0-9_-]+\.txt)$";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
Console.WriteLine(match.Groups[1]);
}
Output
ABC-XYZ-20200914-PIA-03_05_20200914132900.txt
Upvotes: 2