I am using visual c++ tr1
My regex is trying to match “bar”, strings can be
foo bar class
foo;bar
foo; bar
bar foo
bar
the regex I am using was
(^|[; ]+)bar\\s+
This is very slow for some reason, if I change regex to
(^|;|\\s)bar\\s+
It gives me a huge speedup. By the way, I am using regex_search function.
Could anybody explain why is this?
Thanks in advance.
These expressions are not equivilent. The first takes any number of
;semicolons and spaces, while the second takes only one. It might help to see some of the text you are matching.The reason for the performance difference is probably ‘backtracking’. In the first expression,
(^|[; ]+)bar\\s+, there is the potential for backtracking at the begining of the expression. In(^|;|\\s)bar\\s+, there can be no backtracking. Backtracking means reattempting a different path through the data based on a different path through the regex. (However, if it is reallybarthat you are searching for, there shouldn’t be much differnece.)