View Source GlobEx (glob_ex v0.1.11)
Provides glob expressions for Elixir.
A glob expression looks like an ordinary path, except that the following "wildcard characters" are interpreted in a special way:
?
- matches one character.*
- matches any number of characters up to the end of the filename, the next dot, or the next slash.**
- two adjacent*
's used as a single pattern will match all files and zero or more directories and subdirectories.[char1,char2,...]
- matches any of the characters listed; two characters separated by a hyphen will match a range of characters. Do not add spaces before and after the comma as it would then match paths containing the space character itself.{item1,item2,...}
- matches one of the alternatives. Do not add spaces before and after the comma as it would then match paths containing the space character itself.
Other characters represent themselves. Only paths that have
exactly the same character in the same position will match. Note
that matching is case-sensitive: "a"
will not match "A"
.
Directory separators must always be written as /
, even on Windows.
You may call Path.expand/1
to normalize the path before invoking
this function.
A character preceded by loses its special meaning. Note that must be written as \ in a string literal. For example, "\?*" will match any filename starting with ?.
Glob expressions can be created using compile/2
, compile!/2
or the sigils
~g (see GlobEx.Sigils.sigil_g/2
) or ~G (see GlobEx.Sigils.sigil_G/2
).
# A simple glob expressions matching all `.exs` files in a tree. By default,
# the patterns `*` and `?` do not match files starting with a `.`.
~g|**/*.exs|
# The modifier `d` let the glob treat files starting with a `.` as any other
# file.
~g|**/*.exs|d
Summary
Functions
Compiles the glob expression.
Compiles the glob expression and raises GlobEx.CompileError in case of errors.
Traverses paths according to the given glob
expression and returns a list of
matches.
Returns a boolean indicating whether there was a match or not.
Types
Functions
@spec compile( binary(), keyword() ) :: {:ok, t()} | {:error, GlobEx.CompileError.t()}
Compiles the glob expression.
It returns {:ok, regex}
in case of success, {:error, reason}
otherwise.
See the module documentation for how to write a glob expression.
By default, the patterns *
and ?
do not match files starting with a .
.
This behaviour can be change with the option :match_dot
.
Options
:match_dot
- (boolean) iffalse
, the special wildcard characters*
and?
will not match files starting with a.
. Iftrue
, files starting with a.
will not be treated specially. Defaults tofalse
.
Examples
iex> GlobEx.compile("src/**/?oo.ex")
{:ok, ~g|src/**/?oo.ex|}
iex> GlobEx.compile("src/{a,b/?oo.ex")
{:error, %GlobEx.CompileError{reason: {:missing_delimiter, 5}, input: "src/{a,b/?oo.ex"}}
Compiles the glob expression and raises GlobEx.CompileError in case of errors.
See the module documentation for how to write a glob expression and
compile/2
for the available options.
Traverses paths according to the given glob
expression and returns a list of
matches.
See the module documentation for how to write a glob expression.
Examples
iex> GlobEx.ls(~g|{lib,test}/**/*.{ex,exs}|)
[
"lib/glob_ex.ex",
"lib/glob_ex/compiler.ex",
"lib/glob_ex/compiler_error.ex",
"lib/glob_ex/sigils.ex",
"test/glob_ex/compiler_test.exs",
"test/glob_ex_test.exs",
"test/test_helper.exs"
]
Returns a boolean indicating whether there was a match or not.
See the module documentation on how to write a glob expression.
Hidden files starting with a .
will only be matched if match_dot
is
true
or the ~g
/~G
has the modifier d
. However, if the glob expression
consists of an exact path to a hidden file, this path is also matched.
Examples
iex> GlobEx.match?(~g|{lib,test}/**/*.{ex,exs}|, "lib/foo/bar.ex")
true
iex> GlobEx.match?(~g|{lib,test}/**/*.{ex,exs}|, "lib/foo/bar.java")
false
Examples for exact path to a hidden file:
iex> GlobEx.match?(~g|lib/.foo.ex|, "lib/.foo.ex")
true
iex> GlobEx.match?(~g|**/.foo.ex|, "lib/.foo.ex")
false
iex> GlobEx.match?(~g|**/.foo.ex|d, "lib/.foo.ex")
true