A regular expression (for a full match) equivalent to the glob pattern glob.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | glob |
the glob pattern |
function glob_to_regex(glob) result(re) !! A regular expression (for a full match) equivalent to the glob pattern `glob`. character(len=*), intent(in) :: glob !! the glob pattern character(len=:), allocatable :: re integer :: i, j re = '' i = 1 do while (i <= len(glob)) select case (glob(i:i)) case ('*') if (i < len(glob)) then if (glob(i + 1:i + 1) == '*') then if (i + 1 < len(glob)) then if (glob(i + 2:i + 2) == '/') then re = re//'(?:.*/)?' ! `**/`: any number of directories i = i + 3 cycle end if end if re = re//'.*' i = i + 2 cycle end if end if re = re//'[^/]*' case ('?') re = re//'[^/]' case ('[') j = index(glob(i + 1:), ']') if (j > 1) then if (glob(i + 1:i + 1) == '!') then re = re//'[^'//glob(i + 2:i + j) else re = re//glob(i:i + j) end if i = i + j + 1 cycle end if re = re//'\[' case default re = re//re_escape(glob(i:i)) end select i = i + 1 end do end function glob_to_regex