Validating file extensions with regex in c ++

1

#include <regex>
#include <iostream>

bool is_source_file(const std::string& source) 
{   
  std::smatch source_match;
  const std::regex pattern("(?:(?:[a-zA-Z0-9_.])?(c|C|h|H|x|X|.?xx|.?XX|.?pp|.?PP|.?++|.?h|.?H|.?tm|.?tml|.?html|p?p|mp3|asp?|jsp|css)?)?");    
  return std::regex_match(source, source_match, pattern); 
}

int main()
{
  std::string file1="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file1.c";
  std::string file2="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file2.cpp";
  std::string file3="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file3.cxx";
  std::string file4="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file4.C";
  std::string file5="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file5.CPP";
  std::string file6="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file6.CXX";
  std::string file7="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file7.h";
  std::string file8="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file8.hh";
  std::string file9="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file9.hpp";
  std::string file10="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file10.hxx";
  std::string file11="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file11.H";
  std::string file12="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file12.HH";  
  std::string file13="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file13.HPP";
  std::string file14="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file14.HXX";
  std::string file15="/run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file15.aa";


  std::cout<<"\n\tFile: "<<file1<<(is_source_file(file1) ? " is Source File"## Cabeçalhos ##: " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file2<<(is_source_file(file2) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file3<<(is_source_file(file3) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file4<<(is_source_file(file4) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file5<<(is_source_file(file5) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file6<<(is_source_file(file6) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file7<<(is_source_file(file7) ? " is Source File" : " not Source File") << std::endl;  
  std::cout<<"\n\tFile: "<<file8<<(is_source_file(file8) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file9<<(is_source_file(file9) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file10<<(is_source_file(file10) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file11<<(is_source_file(file11) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file12<<(is_source_file(file12) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file13<<(is_source_file(file13) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file14<<(is_source_file(file14) ? " is Source File" : " not Source File") << std::endl;
  std::cout<<"\n\tFile: "<<file15<<(is_source_file(file15) ? " is Source File" : " not Source File") << std::endl;
}

The output is:

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file1.c is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file2.cpp is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file3.cxx is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file4.C is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file5.CPP is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file6.CXX is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file7.h is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file8.hh is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file9.hpp is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file10.hxx is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file11.H is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file12.HH is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file13.HPP is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file14.HXX is Source File

    File: /run/media/anon/MHDD/Cursos/Apostilas_C_and_CPP/CPP/regex/testefile/file15.aa is Source File //aqui esta o problema ele nao pode validar esta extensao
    
asked by anonymous 22.07.2017 / 19:11

1 answer

1

The problem is in the pattern chosen. If you want to filter only file extensions that end with h , hpp , hxx , H , HPP , HXX , c , cpp , cxx , C CPP , CXX , aspx , php , py , java , rb , d htm , html follows the rule to be used:

const std::regex pattern(".*\.(h|hpp|hxx|H|HPP|HXX|c|cpp|cxx|C|CPP|CXX|aspx|php|py|java|rb|d|htm|html|HTM|HTML|mp3|css)");

This rule basically filters all strings that do not end with "dot + extension".

    
26.07.2017 / 00:45