I have in class around 100 Regex
calls, every call cover different type of data in text protocol, but i have many files and based on analytics regex
took 88% of execution of my code.
Many this type of code:
{
Match m_said = Regex.Match(line, @"(.*) said,", RegexOptions.IgnoreCase);
if (m_said.Success)
{
string playername = ma.Groups[1].Value;
// some action
return true;
}
}
{
Match ma = Regex.Match(line, @"(.*) is connected", RegexOptions.IgnoreCase);
if (ma.Success)
{
string playername = ma.Groups[1].Value;
// some action
return true;
}
}
{
Match ma = Regex.Match(line, @"(.*): brings in for (.*)", RegexOptions.IgnoreCase);
if (ma.Success)
{
string playername = ma.Groups[1].Value;
long amount = Detect_Value(ma.Groups[2].Value, line);
// some action
return true;
}
}
Is any way to replace Regex
with some other faster solution?
For regexps that are tested in loop, it is often faster to precompile them outside of the loop and just test them inside of the loop.
You need to declare the different regexps first with their respective patterns and only call the Match()
with the text to test in a second step.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With