Doubt about regex speed.

Hi All, I have one system that receive many messages (TCP/IP transport) on one second, and I validate these messages whit a sequence of if,s and switchs, see exemple of message: 19092;1;12345678;1111222233334444;123;0512;10000;1;0 I would like know if I use regex to do this I will have improvement in performance of my system, or I will have delay in my system? My regex to validate this message is: "\\d{1,5};1;\\d{1,8};\\d{16};\\d{3};\\d{4};\\d{1,8};([0-9]|10|11|12);[012]" My code sample (in old version I need same about 300 lines to do the same): class CParser { public: CParser(const std::string& toparser, const RWDBDatabase &database) { if(validate("\\d{1,5};1;\\d{1,8};\\d{16};\\d{3};\\d{4};\\d{1,8};([0-9]|10|11 |12);[012]", toparser)) { CContext<CProcessMessage1> process; } else if(validate("\\d{1,5};3;\\d{16};\\d{11};[01]", toparser)) { } else if(validate("\\d{1,5};5;\\d{16};\\d{11}", toparser)) { } else if(validate("\\d{1,5};7;\\d{16};\\d{11}", toparser)) { } else { throw CParserException("bad_message"); } } protected: bool validate(const std::string& regex, const std::string& to_validate) { try { const boost::regex e(regex); return regex_match(to_validate.c_str(), e); } catch (...) { throw CParserException("bad_regex_syntax"); } } }; Thanks to help _____ Esta mensagem foi verificada pelo E-mail Protegido Terra <http://mail.terra.com.br/> . Scan engine: McAfee VirusScan / Atualizado em 21/11/2005 / Versão: 4.4.00/4633 Proteja o seu e-mail Terra: http://mail.terra.com.br/

I would like know if I use regex to do this I will have improvement in performance of my system, or I will have delay in my system?
My regex to validate this message is: "\\d{1,5};1;\\d{1,8};\\d{16};\\d{3};\\d{4};\\d{1,8};([0-9]|10|11|12);[012]"
My code sample (in old version I need same about 300 lines to do the same):
Well it probably depends on the quality of your existing code :-) You shouldn't see any noticable slow down, especially if you re-use match_results structures: almost all the overhead is in allocating memory for the array of results, so as long as you re-use the match_results structure it should be pretty quick. I had a quick look at your regular expression and that looks as good as it's going to get as well. The short answer though is to try it out :-) Regards, John.

This may be a silly question, but why would you want to instantiate the same fixed reg-ex more than once, which you are doing in your validate functions. Create one instance of it and reuse it! Michael Goldshteyn
participants (3)
-
John Maddock
-
Michael Goldshteyn
-
Renato Tegon Forti