Outils d'utilisateurs

Outils du Site


validation_motifs

Différences

Cette page vous donne les différences entre la révision choisie et la version actuelle de la page.

Lien vers cette vue

validation_motifs [2014/09/03 23:20]
gbdivers créée
validation_motifs [2016/07/05 18:54] (Version actuelle)
gbdivers
Ligne 1: Ligne 1:
  
-^ Chapitre précédent ^ [[programmez_avec_le_langage_c|Sommaire principal]] ^ Chapitre suivant ^+[[expressions_regulieres_3|Chapitre précédent]] ^ [[programmez_avec_le_langage_c|Sommaire principal]] ^ [[fonctions|Chapitre suivant]] ^
  
-====== Regex - suite ======+====== [Aller plus loin] Les expressions régulières 4 ======
  
 ====== Valider qu'une chaîne correspond à un motif ====== ====== Valider qu'une chaîne correspond à un motif ======
Ligne 24: Ligne 24:
 __ exemples : https://support.google.com/a/answer/1371417?hl=fr __ __ exemples : https://support.google.com/a/answer/1371417?hl=fr __
  
-^ Chapitre précédent ^ [[programmez_avec_le_langage_c|Sommaire principal]] ^ Chapitre suivant ^ 
  
-{{tag&gtCours C++}}+ 
 +<code cpp main.cpp> 
 +    { 
 +        std::regex pattern("(ab)cd(ef)");    // Find double word. 
 +        std::string replacement = "le premier groupe est $1 et le second groupe est $2";  
 +        std::string target = "abcdef"; 
 +        std::string output_str = regex_replace(target, pattern, replacement); 
 +        std::cout << output_str << std::endl; 
 +    } 
 +    { 
 +        std::regex pattern(R&quot;((\d{2})[-/](\d{2})[-/](\d{4}))"); 
 +        std::smatch match; 
 +        std::regex_search(std::string("12-03-2014"), match, pattern); 
 +        for (size_t i = 0; i < match.size(); ++i)  
 +        { 
 +            std::cout << i << ": " << match[i].str() << '\n'; 
 +        }   
 +    } 
 +      
 +    std::cout << "Traduction" << std::endl; 
 +    { 
 +        std::regex pattern("([a-zA-Z]+) \\1");     
 +        std::string replacement = "$1";      
 +        std::string target = "The cat cat bites the dog dog."; 
 +        std::string output_str = regex_replace(target, pattern, replacement); 
 +        std::cout << output_str << std::endl; 
 +    } 
 +     
 +    std::cout << tr("bla bla $1 bla bla", 123) << std::endl; 
 +    std::cout << tr("bli bli bli bli $1", 123) << std::endl; 
 +    std::cout << std::endl; 
 +     
 +     
 +    std::cout << "Groupe" << std::endl; 
 +    match("", R"((ab)*)"); 
 +    match("a", R"((ab)*)"); 
 +    match("b", R"((ab)*)"); 
 +    match("ab", R"((ab)*)"); 
 +    match("abc", R"((ab)*)"); 
 +    match("ababab", R"((ab)*)"); 
 +    std::cout << std::endl; 
 +     
 +     
 +    match("cat", R"(c[a-z]*t)"); 
 +    std::cout << std::endl; 
 +     
 +    std::cout << "Exemples de regex" << std::endl; 
 +    std::cout << "Date" << std::endl; 
 +    match("12-03-2014", R"(\d{2}[-/]\d{2}[-/]\d{4})"); 
 +    std::cout << "Time" << std::endl; 
 +    match("15:17", R"(\d{2}:\d{2})"); 
 +    std::cout << std::endl; 
 +     
 +    // vérifier qu'un identifiant C++ est valide 
 +    // [a-zA-Z_][a-zA-Z_0-9]* 
 +     
 +    // fichier windows 
 +    //[a-zA-Z_][a-zA-Z_0-9]*\.[a-zA-Z0-9]+ 
 +     
 +    std::string regex_str = "[a-z_][a-z_0-9]*\\.[a-z0-9]+"; 
 +    std::regex reg1(regex_str, std::regex_constants::icase); 
 +    std::string str = "File names are readme.txt and my.cmd."; 
 +    std::sregex_iterator it(str.begin(), str.end(), reg1); 
 +    std::sregex_iterator it_end; 
 +    while(it != it_end) { 
 +        std::cout << it->str() << std::endl; 
 +        ++it; 
 +    } 
 +} 
 +</code> 
 + 
 +<code> 
 +Groups 
 +le premier groupe est ab et le second groupe est ef 
 +0: 12-03-2014 
 +1: 12 
 +2: 03 
 +3: 2014 
 +Traduction 
 +The cat bites the dog. 
 +bla bla 123 bla bla 
 +bli bli bli bli 123 
 + 
 +Groupe 
 +"(ab)*" match with "" = true 
 +"(ab)*" match with "a" = false 
 +"(ab)*" match with "b" = false 
 +"(ab)*" match with "ab" = true 
 +"(ab)*" match with "abc" = false 
 +"(ab)*" match with "ababab" = true 
 + 
 +"c[a-z]*t" match with "cat" = true 
 + 
 +Exemples de regex 
 +Date 
 +"\d{2}[-/]\d{2}[-/]\d{4}" match with "12-03-2014" = true 
 +Time 
 +"\d{2}:\d{2}" match with "15:17" = true 
 + 
 +readme.txt 
 +my.cmd 
 +</code> 
 + 
 +===== Manque : vorace ===== 
 + 
 +__ A déplacer dans le chapitre sur les recherches ? __ 
 + 
 +<code cpp main.cpp>     
 +     // non-greedy (non vorace) 
 +    search("aaaaa", R"(a{3})");     // -> aaaaa = plus longue correspondance 
 +    search("aaaaa", R"(a{3}?)");    // -> aaa = plus courte correspondance 
 +    std::cout << std::endl; 
 +
 +</code> 
 + 
 +affiche : 
 + 
 +<code> 
 +search "a{3}" in "aaaaa" = true 
 +search "a{3}?" in "aaaaa" = true 
 +</code> 
 + 
 +^ [[expressions_regulieres_3|Chapitre précédent]] ^ [[programmez_avec_le_langage_c|Sommaire principal]] ^ [[fonctions|Chapitre suivant]] ^ 
validation_motifs.1409779248.txt.gz · Dernière modification: 2014/09/03 23:20 par gbdivers