The best thing? Do not use regular expressions. In particular, in a simplified case of string searches like this.
First, there are a couple of simple optimizations that can be done based on the parameters of the question:
- Since the extension of the input
string must be: ".txt", we do not need to check whether the extension is ".txtTEMP" - The only thing that does not meet the condition when the
string input ends with "_test.txt" requires checking that the trunk ends with "_test", since the extension is already known as ".txt"
Both of these checks will always be offset by a fixed number of characters from the end of the string input. Since all the information for both of these expressions is known, it must be set at compile time:
constexpr auto doMatch = ".txt"; constexpr auto doMatchSize = strlen(doMatch); constexpr auto doNotMatch = "_test"; constexpr auto doNotMatchSize = strlen(doNotMatch) + doMatchSize;
Given a string input , it can be tested for success as follows:
if(input.size() >= doMatchSize && equal(input.end() - doMatchSize, input.end(), doMatch) && (input.size() < doNotMatchSize || !equal(input.end() - doNotMatchSize, input.end() - doMatchSize, doNotMatch)))
Here you can see a live example: http://ideone.com/7BcyFi
source share