Wild-card String Matching
Edit Page
Report
Scan day: 16 February 2014 UTC
16
Virus safety - good
Description: Fortran 77 code by Clive Page to do wild-card matching to a pattern, which may contain "?" to match any single character, and "*" to match zero or more consecutive characters of any type.
program test_match implicit none integer :: np, ns parameter (np = 8, ns = 5) character :: pattern(np)*8, string(ns)*12 integer :: s, p logical :: match_wild, res external match_wild data pattern / '*', 'a*a', 'a*', 'ab*', '*a', 'a*a', 'a?d?', 'a?d*' / data string / 'abracadabra', 'aldabra', 'alda', 'carta', 'abdc' / write(*,'(t18, 100a6)') pattern do s = 1,ns write(*, '(a, 100L6)') string(s), & (match_wild(pattern(p), string(s)), p=1,np) end do end program test_match ! Output should resemble this: ! * a*a a* ab* *a a*a a?d? a?d* ! abracadabra T T T T T T F F ! aldabra T T T F T T T T ! alda T T T F T T T T ! carta T F F F T F F F ! abdc T F T T F F T T !-------------------------------------------------------------------------- ! match_wild: compares given string for match to pattern which may ! contain wildcard characters: ! "?" matching any one character, and ! "*" matching zero or more characters. ! Both strings may have trailing spaces which are ignored. ! Author: Clive Page, userid: cgp domain: le.ac.uk, 2003 June 24. ! Note: can be compiled with GNU g77 -ffree-form ! logical function match_wild(pattern, string) implicit none character(*) :: pattern ! input: pattern may contain * and ? character(*) :: string ! input: string to be compared ! integer :: lenp, lens, n, p, s ! lenp = len_trim(pattern) lens = len_trim(string) p = 1 s = 1 match_wild = .false. do if(pattern(p:p) == '?') then ! accept any char in string p = p + 1 s = s + 1 else if(pattern(p:p) == "*") then p = p + 1 if(p > lenp) then ! anything goes in rest of string match_wild = .true. EXIT else if(p == lenp) then ! just check last char of string match_wild = pattern(p:p) == string(lens:lens) EXIT else ! search string for char at p n = index(string(s:), pattern(p:p)) if(n == 0) EXIT ! no such char, exit false s = n + s - 1 end if else if(pattern(p
Size: 2048 chars
Contact Information
Email: —
Phone&Fax: —
Address: —
Extended: —
WEBSITE Info
Page title: | |
Keywords: | |
Description: | |
IP-address: | 143.210.56.104 |