Besides precompiling your regular expression, you can get (possibly much more) performance benefits by writing a more accurate regular expression. In this regard .* Is almost always a bad choice:
(.*) is connected means: first match the entire string (part .* ) and then cancel one character at a time until it matches with is connected .
Now, if the line is not very short, or is connected displayed very close to the end of the line, this is a lot back, which costs time.
So, if you can clarify what an allowed match is, you can improve performance.
For example, if only alphanumeric characters are allowed, then (\w+) is connected will be good. If these are any characters without spaces, use (\S+) is connected . Etc., depending on the rules for valid compliance.
In your specific example, you don't seem to be doing anything with a captured match, so you can even add a regular expression at all and just look for a fixed substring. Which method will be the fastest at the end depends on your actual input and requirements.
source share