View Source GlobEx (glob_ex v0.1.8)

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 in 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

@type t() :: %GlobEx{compiled: [term()], match_dot: boolean(), source: binary()}

Functions

Link to this function

compile(glob, opts \\ [])

View Source
@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) if false, the special wildcard characters * and ? will not match files starting with a .. If true, files starting with a . will not be treated specially. Defaults to false.

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}}}
Link to this function

compile!(glob, opts \\ [])

View Source
@spec compile!(
  binary(),
  keyword()
) :: t()

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.

@spec ls(t()) :: [Path.t()]

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"
]
@spec match?(t(), Path.t()) :: boolean()

Returns a boolean indicating whether there was a match or not.

See the module documentation for how to write a glob expression.

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