1
personnel I made a function to validate extensions of files using the regex of c++ however it is validating extensions outside the regex I tried to do something to change it but could not get anyone to help me?
#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 exit 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
I believe his
pattern
contains errors, can give an example of the output you want, or which files should not be listed...– MagicHat
according to the Pattern("(?:(?:[a-za-Z0-9_.])?(c|C|h|H|x|X|.?XX|.?XX|.?PP|.???PP|.?+|.?h|.?H|.???H|.?????HTML|p?p|mp3|Asp?|jsp|css)?)?");; any extension outside of it could not be validated.. only this out and yet it is validating File: /run/media/Anon/MHDD/Courses/Apostilas_c_and_cpp/CPP/regex/testefile/file15.aa is Source File //here is the problem it cannot validate this extensive
– dark777
Forget the
pattern
that it must be wrong, which is the list of extensions you want to capture?– MagicHat
h,hpp,hxx,H,HPP,HXX,c,cpp,Cxx,C,CPP,CXX,aspx,php,py,java,Rb,d,htm,html,HTM,HTML,mp3,css
– dark777