Reputation: 155
i have following problem: I have text like this (more than 200 lines):
dsadsadsads(-123|12)sdakodskoakosdakodsadsayxvmyxcmxcym,§§¨§¨§(-43|23)sdadasdas
I want get numbers from text like this:
-123|12
-43|23
Numbers are always in (
)
.
What is the fast way, how to get this number. Is possible use some regex? How?
Or brute force
foor loop?
Thank you for your reply.
Upvotes: 0
Views: 79
Reputation: 19149
You can use Regex for this purpose.
string str = "dsadsadsads(-123|12)sdakodskoakosdakodsadsayxvmyxcmxcym,§§¨§¨§(-43|23)sdadasdas";
var matches = Regex.Matches(str, @"\(([-+]?\d+\|[-+]?\d+)\)");
foreach (var match in matches)
{
Console.WriteLine(match);
}
Performance test
string str = "dsadsadsads(-123|12)sdakodskoakosdakodsadsayxvmyxcmxcym,§§¨§¨§(-43|23)sdadasdas";
StringBuilder bigstr = new StringBuilder();
for (int i = 0; i < 1000; i++)
{
bigstr.Append(str + "\n");
}
str = bigstr.ToString();
Regex regex = new Regex(@"\(([-+]?\d+\|[-+]?\d+)\)");
Stopwatch w = Stopwatch.StartNew();
var matches = regex.Matches(str);
var count = matches.Count;
w.Stop();
Console.WriteLine(w.Elapsed);
Output in my console. about 0.001
seconds.
Upvotes: 1