Validating extensions of regex files in c++

Asked

Viewed 188 times

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...

  • 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

  • Forget the pattern that it must be wrong, which is the list of extensions you want to capture?

  • h,hpp,hxx,H,HPP,HXX,c,cpp,Cxx,C,CPP,CXX,aspx,php,py,java,Rb,d,htm,html,HTM,HTML,mp3,css

1 answer

1


The problem is in 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, HTM, HTML, mp3, css 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".

  • Then I had tried to do this way then gave this mistake now I understood that it was because of a point.. Thanks there...

  • Arrange. Do not forget to accept or vote if the answer is valid.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.