问题
Elixir provides Path.wildcard
, which uses the Erlang :filelib.wildcard
function internally.
Matching is case-sensitive, for example, "a" does not match "A". (http://erlang.org/doc/man/filelib.html#wildcard-1)
Please is there a case-insensitive alternative?
回答1:
There's no built in option to do this, but since the wildcard syntax supports character alternations similar to regex, you can replace every letter with an alternation of its lower and upper case versions, e.g. f0o
-> [fF]0[oO]
, and then pass that to Path.wildcard/1
. Here's a simple implementation that does this for ASCII letters:
defmodule A do
def case_insensitive_glob(glob) do
Regex.replace(~r/[a-zA-Z]/, glob, fn letter ->
"[#{String.downcase(letter)}#{String.upcase(letter)}]"
end)
end
end
glob = A.case_insensitive_glob("**/*reAdmE.*") |> IO.inspect
Path.wildcard(glob) |> IO.inspect
Running this in the OTP source code produces all files with their name containing "reAdmE." in any case.
"**/*[rR][eE][aA][dD][mM][eE].*"
["README.md", "erts/emulator/pcre/README.pcre_update.md",
"lib/erl_interface/src/README.internal",
"lib/ic/examples/pre_post_condition/ReadMe.txt", "xcomp/README.md"]
I've verified the output's correctness with find
:
$ find . -iname 'readme.*'
./erts/emulator/pcre/README.pcre_update.md
./lib/erl_interface/src/README.internal
./lib/ic/examples/pre_post_condition/ReadMe.txt
./README.md
./xcomp/README.md
来源:https://stackoverflow.com/questions/45597018/how-to-perform-a-case-insensitive-file-search-in-erlang-elixir