Jump to content

Regex para validar um string


Leudassdf
 Share

Recommended Posts

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

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 by HappyHippyHippo
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.