glob目录点_在目录中使用Glob

glob目录点

"Globbing" files (with Dir.glob) in Ruby allows you to select just the files you want, such as all the XML files, in a given directory. Even though Dir.blog is like regular expressions, it is not. It's very limited compared to Ruby's regular expressions and is more closely related to shell expansion wildcards.

Ruby中的“ Globbing ”文件(带有Dir.glob )使您可以在给定目录中仅选择所需的文件,例如所有XML文件。 即使Dir.blog 就像正则表达式,也不是。 与Ruby的正则表达式相比,它非常受限制,并且与shell扩展通配符更紧密相关。

The opposite of globbing, iterating over all the files in a directory, can be done with the Dir.foreach method.

使用Dir.foreach方法可以完成遍历相反的操作,即遍历目录中的所有文件。

( Example )

The following glob will match all files ending in .rb in the current directory. It uses a single wildcard, the asterisk. The asterisk will match zero or more characters, so any file ending in .rb will match this glob, including a file called simply .rb, with nothing before the file extension and its preceding period. The glob method will return all files that match the globbing rules as an array, which can be saved for later use or iterated over.

以下glob将匹配当前目录中所有以.rb结尾的文件。 它使用单个通配符星号。 星号将匹配零个或多个字符,因此任何以.rb结尾的文件都将匹配此glob,包括名为.rb的文件,在文件扩展名及其前一个期间之前没有任何内容。 glob方法将所有与globbing规则匹配的文件作为数组返回,可以将其保存以备后用或迭代。

 #!/usr/bin/env ruby
 Dir.glob('*.rb').each do|f|
 puts f
 end

通配符及更多 ( Wildcards and More )

There are only a few wildcards to learn:

仅需学习一些通配符:

  • * – Match zero or more characters. A glob consisting of only the asterisk and no other characters or wildcards will match all files in the current directory. The asterisk is usually combined with a file extension if not more characters to narrow down the search.

    * –匹配零个或多个字符。 仅由星号组成且没有其他字符或通配符的全局名称将匹配当前目录中的所有文件。 如果没有更多字符,星号通常与文件扩展名结合使用以缩小搜索范围。

  • ** – Match all directories recursively. This is used to descend into the directory tree and find all files in sub-directories of the current directory, rather than just files in the current directory. This wildcard is explored in the example code below.

    ** –递归匹配所有目录。 它用于下降到目录树中并在当前目录的子目录中查找所有文件,而不仅仅是在当前目录中的文件。 在下面的示例代码中对此通配符进行了研究。

  • ? – Match any one character. This is useful for finding files whose name are in a particular format. For example, 5 characters and a .xml extension could be expressed as ?????.xml.

    –匹配任意一个字符。 这对于查找名称具有特定格式的文件很有用。 例如,5个字符和一个.xml扩展名可以表示为?????。xml

  • [a-z] – Match any character in the character set. The set can be either a list of characters or a range separated with the hyphen character. Character sets follow the same syntax as and behave in the same manner as character sets in regular expressions.

    [az] –匹配字符集中的任何字符。 该集合可以是字符列表,也可以是用连字符分隔的范围。 字符集与正则表达式中的字符集遵循相同的语法并以相同的方式运行。

  • {a,b} – Match pattern a or b. Though this looks like a regular expression quantifier, it isn't. For example, in regular expression, the pattern a{1,2} will match 1 or 2 'a' characters. In globbing, it will match the string a1 or a2. Other patterns can be nested inside of this construct.

    {a,b} –匹配模式a或b。 尽管这看起来像是一个正则表达式量词,但事实并非如此。 例如,在正则表达式中,模式a {1,2}将匹配1或2个'a'字符。 在遍历中,它将匹配字符串a1a2 。 其他模式可以嵌套在此构造内部。

One thing to consider is case sensitivity. It's up to the operating system to determine whether TEST.txt and TeSt.TxT refer to the same file. On Linux and other systems, these are different files. On Windows, these will refer to the same file.

要考虑的一件事是区分大小写 。 由操作系统决定TEST.txtTeSt.TxT是否引用相同的文件。 在Linux和其他系统上,这些是不同的文件。 在Windows上,这些将引用相同的文件。

The operating system is also responsible for the order in which the results are displayed. It may differ if you're on Windows versus Linux, for example.

操作系统还负责显示结果的顺序。 例如,如果您使用的是Windows vs Linux ,则可能会有所不同。

One final thing to note is the Dir[globstring] convenience method. This is functionally the same as Dir.glob(globstring) and is also semantically correct (you are indexing a directory, much like an array). For this reason, you may see Dir[] more often than Dir.glob, but they are the same thing.

最后要注意的一件事是Dir [globstring]便捷方法。 这在功能上与Dir.glob(globstring)相同,并且在语义上也是正确的(您正在为目录建立索引,就像数组一样)。 因此,您可能比Dir.glob看到Dir []的频率更高 ,但是它们是同一回事。

使用通配符的示例 ( Examples Using Wildcards )

The following example program will demonstrate as many patterns as it can in many different combinations.

以下示例程序将以许多不同的组合演示尽可能多的模式。

 #!/usr/bin/env ruby
 # Get all .xml files
 Dir['*.xml']
 # Get all files with 5 characters and a .jpg extension
 Dir['?????.jpg']
 # Get all jpg, png and gif images
 Dir['*.{jpg,png,gif}']
 # Descend into the directory tree and get all jpg images
 # Note: this will also file jpg images in the current directory
 Dir['**/*.jpg']
 # Descend into all directories starting with Uni and find all
 # jpg images.
 # Note: this only descends down one directory
 Dir['Uni**/*.jpg']
 # Descend into all directories starting with Uni and all
 # subdirectories of directories starting with Uni and find
 # all .jpg images
 Dir['Uni**/**/*.jpg']

翻译自: https://www.thoughtco.com/using-glob-with-directories-2907832

glob目录点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值