In example string:
string input = "8240617_8240608_8240573\r\n";
i need to get only the last part: 8240573.
In finale application there can be dots and letters. But it will always be end of the line and between "_" and "\r\n".
I tried code below but whit no result 🙁
string input = "8240617_8240608_8240573\r\n";
string pattern = @"-(.*?)\\r\\n";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
string extractedString = match.Groups[1].Value;
Debug.WriteLine("Extracted substring: " + extractedString);
}
else
{
Debug.WriteLine("Substring not found.");
}
>Solution :
Your regex pattern is off. Use this version (and keep the rest of the code the same):
string pattern = @"([^_]+)\r\n";
This regex says to match:
([^_]+)match the last underscore separated term\r\nfollowed by CRLF
Note that in your version you were matching a hyphen, and also it would have picked up across multiple terms. Also, a newline inside an @ string in C# is @"\n", with just a single backslash.