Task: Extract file extension


C++: #include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <regex>

std::string findExtension ( const std::string & filename ) {
   auto position = filename.find_last_of ( '.' ) ;
   if ( position == std::string::npos )
      return "" ;
   else {
      std::string extension ( filename.substr( position + 1 ) ) ;
      if (std::regex_search (extension, std::regex("[^A-Za-z0-9]") ))
         return "" ;
      else
         return extension ;
   }
}

int main( ) {
   std::vector<std::string> filenames {"picture.jpg" , "http:
      "myuniquefile.longextension" , "IAmAFileWithoutExtension" , "/path/to.my/file" ,
      "file.odd_one", "thisismine." } ;
   std::vector<std::string> extensions( filenames.size( ) ) ;
   std::transform( filenames.begin( ) , filenames.end( ) , extensions.begin( ) , findExtension ) ;
   for ( int i = 0 ; i < filenames.size( ) ; i++ )
      std::cout << filenames[i] << " has extension : " << extensions[i] << " !\n" ;
   return 0 ;
}









Fortran:       MODULE TEXTGNASH	
       CHARACTER*10 DIGITS		
       CHARACTER*11 DDIGITS		
       CHARACTER*13 SDDIGITS		
       CHARACTER*4  EXPONENTISH		
       CHARACTER*17 NUMBERISH		
       CHARACTER*16 HEXLETTERS		
       CHARACTER*62 DIGILETTERS		
       CHARACTER*26 LITTLELETTERS,BIGLETTERS	
       CHARACTER*52 LETTERS		
       CHARACTER*66 NAMEISH		
       CHARACTER*3  ODDITIES		
       CHARACTER*1 CHARACTER(72)	
       EQUIVALENCE			
     1  (CHARACTER( 1),EXPONENTISH,NUMBERISH),	
     2  (CHARACTER( 5),SDDIGITS),		
     3  (CHARACTER( 7),DDIGITS,NAMEISH),	
     4  (CHARACTER( 8),DIGITS,HEXLETTERS,DIGILETTERS),	
     5  (CHARACTER(18),BIGLETTERS,LETTERS),	
     6  (CHARACTER(44),LITTLELETTERS),		
     7  (CHARACTER(70),ODDITIES)		
       DATA EXPONENTISH /"eEdD"/	
       DATA SDDIGITS /"+-.0123456789"/	
       DATA BIGLETTERS    /"ABCDEFGHIJKLMNOPQRSTUVWXYZ"/	
       DATA LITTLELETTERS /"abcdefghijklmnopqrstuvwxyz"/	
       DATA ODDITIES /"_:#"/		

       CHARACTER*62 GOODEXT	
       EQUIVALENCE (CHARACTER(8),GOODEXT)
c       PARAMETER (GOODEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"	
c     1                    //"abcdefghijklmnopqrstuvwxyz"	
c     2                    //"0123456789")			
       INTEGER MEXT		
       PARAMETER (MEXT = 28)	
       CONTAINS
        CHARACTER*(MEXT) FUNCTION FEXT(FNAME)	
         CHARACTER*(*) FNAME	
         INTEGER L1,L2		
          L2 = LEN(FNAME)	
          L1 = L2		
   10     IF (L1.GT.0) THEN	
            IF (INDEX(GOODEXT,FNAME(L1:L1)).GT.0) THEN	
              L1 = L1 - 1		
              GO TO 10			
            END IF		
            IF (FNAME(L1:L1).EQ.".") THEN	
              L1 = L1 - 1			
              GO TO 20				
            END IF		
          END IF		
          L1 = L2		
   20     FEXT = FNAME(L1 + 1:L2)	
        END FUNCTION FEXT	
      END MODULE TEXTGNASH	

      PROGRAM POKE
      USE TEXTGNASH

      WRITE (6,*) FEXT("Picture.jpg")
      WRITE (6,*) FEXT("http://mywebsite.com/picture/image.png")
      WRITE (6,*) FEXT("myuniquefile.longextension")
      WRITE (6,*) FEXT("IAmAFileWithoutExtension")
      WRITE (6,*) FEXT("/path/to.my/file")
      WRITE (6,*) FEXT("file.odd_one")
      WRITE (6,*)
      WRITE (6,*) "Now for the new test collection..."
      WRITE (6,*) FEXT("http://example.com/download.tar.gz")
      WRITE (6,*) FEXT("CharacterModel.3DS")
      WRITE (6,*) FEXT(".desktop")
      WRITE (6,*) FEXT("document")
      WRITE (6,*) FEXT("document.txt_backup")
      WRITE (6,*) FEXT("/etc/pam.d/login")
      WRITE (6,*) "Approved characters: ",GOODEXT
      END