I referred to Regular expressions in C: examples?
It seems that the regex has to be "compiled" before using. Why does this needs to be explicitly done? Why can't 'pcre_exec' do the job itself?
I referred to Regular expressions in C: examples?
It seems that the regex has to be "compiled" before using. Why does this needs to be explicitly done? Why can't 'pcre_exec' do the job itself?
It's a design decision.
It could, but if it did the compilation and the execution in one step, then it would be quite inefficient to use the same regex multiple times. The compilation of a regex is a computationally expensive operation (just like compiling some source code written in a programming language is expensive), so if you want to use the regex more than once, then doing
expensive_compilation(regex_object, "/the/regular\.expression$");
for (i = 0; i < 1000000; i++)
regex_match(regex_object, next_line_to_be_processed);
will be significantly faster than if you moved the (redundant) compilation inside the loop.