When C++ source is compiled the errors are shown in this format
cPlusPlusSource.cpp:row_number:column_number: error: Error description text

What should the regular expression for this be ?

Please help!

Recommended Answers

All 9 Replies

I wish to extract the source name, row_number, column_number, error and error description text ..... !

Member Avatar for diafol

Probably be best to just explode() using ':' to get an array of values. But I suppose you need to find it first right?

Member Avatar for diafol

I'm no regicist, but I'll have a go -

preg_match("/(.+\.cpp):(.+):(.+):(.+):(.+)/",$str,$match);
array_shift($match);
print_r(array_map("trim",$match));

Works for me with the format given. Doubtless there's a nicer way.

There were some compilation errors! Terminating program execution...
The errors are as follows:
20121203.cpp: In function 'int main()':
20121203.cpp:3:2: error: 'inti' was not declared in this scope
20121203.cpp:6:1: error: expected ';' before '}' token

This is the error text that comes up. Actually I have made an Online IDE for c++ in php. I redirected the output of the console to the web page as text using pipelines/proc_open. I need to extract all the errors seperataly

preg_match("/(.+\.cpp):[0-9]:[0-9]:(.+):(.+)/",$str,$match);
array_shift($match);
print_r(array_map("trim",$match));

This would also work .... !

When I apply this regex to the text mentioned above :
I get this as output
Array ( [0] => 20121203.cpp:3:2: error: 'inti' was not declared in this scope [1] => 20121203.cpp [2] => error [3] => 'inti' was not declared in this scope )

But how to get the output with 20121203.cpp:3:2: error: 'inti' was not declared in this scope and 20121203.cpp:6:1: error: expected ';' before '}' token separately ??

Oh! sorry ....
I had to use preg_match_all instead of preg_match .... !

preg_match_all("/(.+\.cpp):[0-9]*:[0-9]*:(.+):(.+)/",$str,$match);

This would be a better option to be used!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.