File Glob

lua-users home

The Lua standard library, like ANSI C, lacks a built-in function for file globbing (see [Wikipedia:Glob]), which is to match a expression such hello[a-z]??.* against a filename. This page describes some approaches to support this in Lua.

Globs have had a number of syntaxes. See

The glob function implemented by Windows FindFileFirst/FindFileNext API calls can be especially counterintuitive and should sometimes be avoided (e.g. *.txt may match both 1.txt and 1.txt~). So, wrapping these API calls in a Lua extension DLL may not be the most desirable approach.

Solution: Pure Lua

The following approach converts a glob expression into a Lua pattern, which can then be used in Lua's pattern matching function. Roughly in concept, ignoring differences in syntax, globs are a subset of Lua patterns which are a subset of regular expressions. However, globs sometimes have some obscure rules and corner cases (see above) depending on chosen syntax.

source code:


Solution: Posix

Posix specifies that compilant systems provide a glob system call: see any such man page, e.g., for details.

The [luaposix] module interfaces to this library. --CharlesStewart?

Solution: Wrap APR fnmatch

See the [apr.fnmatch()] function in LuaApr, which wraps the APR's fnmatch function.

Other Examples

See also scite_Files in SciteExtMan. Also note [Perl glob].

RecentChanges · preferences
edit · history
Last edited April 10, 2013 6:41 am GMT (diff)