Leudassdf Posted December 27, 2015 at 03:19 PM Report Share #591074 Posted December 27, 2015 at 03:19 PM Boas pessoal, Actualmente tenho a seguinte expressão para validar strings mas o meu problema é que a mesma é muito lenta. regex e("(\\w+ ?)+ *$"); return regex_match(conteudo, e); Gostaria de saber se alguem me consegue arranjar uma expressão mais eficiente. A expressão deve: Impedir um espaço no inicio e no fim Impedir mais que um espaço entre palavras Impedir virgulas Cumprimentos, Leandro Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted December 27, 2015 at 05:45 PM Report Share #591076 Posted December 27, 2015 at 05:45 PM o que consideras "muito lento" ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Leudassdf Posted December 27, 2015 at 06:47 PM Author Report Share #591077 Posted December 27, 2015 at 06:47 PM (edited) o que consideras "muito lento" ? Considero que para fazer uma validação a uma string do tipo "Amanha de1256" p.ex, demora vários segundos. Edited December 27, 2015 at 07:18 PM by Leudassdf Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted December 27, 2015 at 10:02 PM Report Share #591080 Posted December 27, 2015 at 10:02 PM vários segundos ? isso cheira a que o problema esteja nutro lugar qualquer ... IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Leudassdf Posted December 27, 2015 at 10:52 PM Author Report Share #591081 Posted December 27, 2015 at 10:52 PM vários segundos ? isso cheira a que o problema esteja nutro lugar qualquer ... Pelos testes que vi é mesmo da "string" de validação. se trocar por outra é "instantâneo". O problema é que a outra não faz bem a validação que quero Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted December 28, 2015 at 10:32 AM Report Share #591085 Posted December 28, 2015 at 10:32 AM (edited) antes de mais, fiz um pequeno teste e, a mim, o teste é instantâneo. segundo, aqui fica o teste que fiz: #include <iostream> #include <string> #include <regex> int main () { try { std::string subjects[] = {"word", " word", "word ", "word word", " word word", "word word ", "word word", " word word", "word word "}; { std::regex exp<b></b>ression("(\\w+ ?)+ *$"); for (auto subject : subjects) std::cout << std::string(std::regex_search(subject, exp<b></b>ression) ? " valid" : "invalid") << " : [" + subject + "]" << std::endl; } std::cout << std::endl; { std::regex exp<b></b>ression("^(\\w+\\s)*\\w+$"); for (auto subject : subjects) std::cout << std::string(std::regex_search(subject, exp<b></b>ression) ? " valid" : "invalid") << " : [" + subject + "]" << std::endl; } } catch (std::regex_error& error) { switch (error.code()) { case std::regex_constants::error_collate: std::cerr << "The exp<b></b>ression contained an invalid collating element name." << std::endl; break; case std::regex_constants::error_ctype: std::cerr << "The exp<b></b>ression contained an invalid character class name." << std::endl; break; case std::regex_constants::error_escape: std::cerr << "The exp<b></b>ression contained an invalid escaped character, or a trailing escape." << std::endl; break; case std::regex_constants::error_backref: std::cerr << "The exp<b></b>ression contained an invalid back reference." << std::endl; break; case std::regex_constants::error_brack: std::cerr << "The exp<b></b>ression contained mismatched brackets ([ and ])." << std::endl; break; case std::regex_constants::error_paren: std::cerr << "The exp<b></b>ression contained mismatched parentheses (( and ))." << std::endl; break; case std::regex_constants::error_brace: std::cerr << "The exp<b></b>ression contained mismatched braces ({ and })." << std::endl; break; case std::regex_constants::error_badbrace: std::cerr << "The exp<b></b>ression contained an invalid range between braces ({ and })." << std::endl; break; case std::regex_constants::error_range: std::cerr << "The exp<b></b>ression contained an invalid character range." << std::endl; break; case std::regex_constants::error_space: std::cerr << "There was insufficient memory to convert the exp<b></b>ression into a finite state machine." << std::endl; break; case std::regex_constants::error_badrepeat: std::cerr << "The exp<b></b>ression contained a repeat specifier (one of *?+{) that was not preceded by a valid regular exp<b></b>ression." << std::endl; break; case std::regex_constants::error_complexity: std::cerr << "The complexity of an attempted match against a regular exp<b></b>ression exceeded a pre-set level." << std::endl; break; case std::regex_constants::error_stack: std::cerr << "There was insufficient memory to determine whether the regular exp<b></b>ression could match the specified character sequence." << std::endl; break; default: std::cerr << "Some other regex exception happened." << std::endl; break; } } return 0; } com o seguinte resultado: valid : [word] valid : [ word] valid : [word ] valid : [word word] valid : [ word word] valid : [word word ] valid : [word word] valid : [ word word] valid : [word word ] valid : [word] invalid : [ word] invalid : [word ] valid : [word word] invalid : [ word word] invalid : [word word ] invalid : [word word] invalid : [ word word] invalid : [word word ] Edited December 28, 2015 at 10:32 AM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now