I want to find and get a string after a string known values in a text file with c#
My text file:
function PreloadFiles takes nothing returns nothing
call Preload( "=== Save ===" )
call Preload( "Player: Michael" )
call Preload( "-load1 UvjkiJyjLlPN1o7FCAwQ0en80t769u5uBKAL1t0u0Cajk86WNmp83F" )
call Preload( "-load2 IMdOIPKGSDFXStx4Zd4LAvAaBmHW19rxsvSNF6kaObSFyBzGq8skYGuq0T1eW" )
call Preload( "-load3 Bd6MoyqnfDydBbwqGApWii3mabJpwNvjcwrKLI0r6UU2wadrMV1h7WQ8D6" )
call Preload( "-load4 D5kI18Flk5bJ4Oi7vQw33b5LHDXHGgJNYsiC6VNJDAHe1" )
call Preload( "KEY PASS: 3568" )
endfunction
i want to get string after string "-load1" ,"-load2" ,"-load3" ,"-load4" ,"KEY PASS: " and fill them on 5 Textbox
like that
UvjkiJyjLlPN1o7FCAwQ0en80t769u5uBKAL1t0u0Cajk86WNmp83F
IMdOIPKGSDFXStx4Zd4LAvAaBmHW19rxsvSNF6kaObSFyBzGq8skYGuq0T1eW
Bd6MoyqnfDydBbwqGApWii3mabJpwNvjcwrKLI0r6UU2wadrMV1h7WQ8D6
D5kI18Flk5bJ4Oi7vQw33b5LHDXHGgJNYsiC6VNJDAHe1
3568
Please help me
Thanks you!
CodePudding user response:
you can use
string Substring (int startIndex);
like:
string in1 = "-load1 UvjkiJyjLlPN1o7FCAwQ0en80t769u5uBKAL1t0u0Cajk86WNmp83F";
string out = in1.substring(7);
it returns:
"UvjkiJyjLlPN1o7FCAwQ0en80t769u5uBKAL1t0u0Cajk86WNmp83F"
CodePudding user response:
It is possible to do with Regex
class (from System.Text.RegularExpressions
namespace).
Patterns examples:
- for
-loadN ...
string:" [A-Za-z0-9]*\" "
. It means Regex should look for substring which starts with whitespace" "
contains some amount of chars(A-z)
(of any case) or digits(0-9)
and ends with double quote\"
and whitespace" "
. Such as yoursUvjkiJyjLlP..."
. - for
KEY PASS: ...
string:@"KEY PASS: (\d{4})"
. This means Regex should find a substring which contains"KEYPASS: "
text and somestring of 4 digits
and with whitespace" "
between them.
But aware, it's very unsafe, because Regex patterns is very sensitive. For example,
"-loaddd1 AbCdEfG..."
(extra chars)"-load1 AbCdEfG..."
(multiple whitespaces)"KEY PASS: 12345"
(pattern in example below looks strictly only for 4 digits, not 5 or more or less)"-LOAD1 AbCdEfG..."
(uppercased) etc.
This ones will be ignored (last, btw, could be solved by passing RegexOptions.IgnoreCase
into Regex.Match(line, pattern, RegexOptions.IgnoreCase)
). Others could be solved too, but you should know that this cases are possible.
For a provided in question example this code works fine:
string loadPattern = " [A-Za-z0-9]*\" ";
string keyPassPattern = @"KEY PASS: (\d{4})";
List<string> capturedValues = new List<string>();
foreach (string line in File.ReadAllLines("Preload.txt"))
{
string s;
if (Regex.IsMatch(line, loadPattern) && line.Contains("-load"))
{
// Getting captured substring and trimming from trailing whitespace and quote
s = Regex.Match(line, loadPattern, RegexOptions.IgnoreCase).Value.Trim('\"', ' ');
capturedValues.Add(s);
}
else if (Regex.IsMatch(line, keyPassPattern))
{
// Just replacing "KEY PASS: " to empty string
s = Regex.Match(line, keyPassPattern).Value.Replace("KEY PASS: ", "");
capturedValues.Add(s);
}
}
Result: