Task: Extract file extension






PHP: $tests = [
    ['input'=>'http://example.com/download.tar.gz', 'expect'=>'.gz'],
    ['input'=>'CharacterModel.3DS', 'expect'=>'.3DS'],
    ['input'=>'.desktop', 'expect'=>'.desktop'],
    ['input'=>'document', 'expect'=>''],
    ['input'=>'document.txt_backup', 'expect'=>''],
    ['input'=>'/etc/pam.d/login', 'expect'=>'']
];

foreach ($tests as $key=>$test) {
    $ext = pathinfo($test['input'], PATHINFO_EXTENSION);


    if (ctype_alnum($ext)) {

        $tests[$key]['actual'] = '.'.$ext;
    } else {
        $tests[$key]['actual'] = '';
    }
}
foreach ($tests as $test) {
    printf("%35s -> %s \n", $test['input'],$test['actual']);
}





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