Task: Extract file extension








VB: Function fileExt(fname)
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set regex = new regExp
        Dim ret

	regex.pattern = "^[A-Za-z0-9]+$"	
	If regex.test(fso.GetExtensionName(fname)) = False Then
		ret = ""
	Else
		ret = "." & fso.GetExtensionName(fname)
	End If
	fileExt = ret
End Function
 

arr_t = Array("http://example.com/download.tar.gz",	_
	      "CharacterModel.3DS",			_
	      ".desktop",				_
	      "document",				_
	      "document.txt_backup",			_
	      "/etc/pam.d/login")
 
For Each name In arr_t
	Wscript.Echo "NAME:",name
	Wscript.Echo " EXT:","<" & fileExt(name) & ">"
Next



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