如何在Erlang / Elixir中执行不区分大小写的文件搜索 [英] How to perform a case-insensitive file search in Erlang/Elixir

查看:146
本文介绍了如何在Erlang / Elixir中执行不区分大小写的文件搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Elixir提供 Path.wildcard ,它在内部使用Erlang :filelib.wildcard 函数。

Elixir provides Path.wildcard, which uses the Erlang :filelib.wildcard function internally.


匹配区分大小写,例如a与A不匹配。
http://erlang.org/doc/man/filelib .html#wildcard-1

请问有不区分大小写的替代方法吗?

Please is there a case-insensitive alternative?

推荐答案

没有内置的选项来执行此操作,但是由于通配符语法支持类似于正则表达式的字符更改,因此可以替换每个字母与其下部和大写版本的交替,例如 f0o - > [fF] 0 [oO] ,然后将其传递给路径。通配符/ 1 。这是一个简单的实现,为ASCII字母执行此操作:

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

在OTP源代码中运行此代码将生成所有包含reAdmE的文件。在任何情况下。

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"]

我已经使用 find 验证输出的正确性:

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

这篇关于如何在Erlang / Elixir中执行不区分大小写的文件搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆