首页 » 信息技术 »

glob模式匹配

2019年2月26日 / 99次阅读
Bash

glob是一个古老的UNIX程序,用来进行文件路径名的模式匹配。在后来发展中,glob模式匹配又有一些扩展,但是基本思路没有变化。glob的模式匹配跟正则表达式不太一样,而且比正则表达式要简单一些。glob的模式匹配也叫做通配符匹配(wildcard matching)。

wildcard matching 通配符匹配

A string is a wildcard pattern if it contains one of the characters
'?', '*' or '['. Globbing is the operation that expands a wildcard
pattern into the list of pathnames matching the pattern. Matching is
defined by:

A '?' (not between brackets) matches any single character.

A '*' (not between brackets) matches any string, including the empty
string.

如果字符串含有?,*,或者[,这三种符号,这个字符串就可以成为是通配符模式。

Globbing(glob的模式匹配)就是扩展通配符模式,使其变为一系列符合模式的路径名的操作。

?:(不在括号内时)匹配任意一个字符;

*:(不在括号内时)匹配任意字符串,包括空串;

Character classes 字符集合

An expression "[...]" where the first character after the leading '['
is not an '!' matches a single character, namely any of the
characters enclosed by the brackets. The string enclosed by the
brackets cannot be empty; therefore ']' can be allowed between the
brackets, provided that it is the first character. (Thus, "[][!]"
matches the three characters '[', ']' and '!'.)

字符集合:[...]这样的表达式,[]不能为空;

[abc123]表示匹配abc123这6个字符的任意一个;

[][!]表示匹配[,],或者!。

Ranges 范围

There is one special convention: two characters separated by '-'
denote a range. (Thus, "[A-Fa-f0-9]" is equivalent to
"[ABCDEFabcdef0123456789]".) One may include '-' in its literal
meaning by making it the first or last character between the
brackets. (Thus, "[]-]" matches just the two characters ']' and '-',
and "[-.0]" matches the three characters '-', '.', '0', since '/'
cannot be matched.)

在[]中使用-,表示一个字符范围;

/不能被匹配;

Complementation 补充

An expression "[!...]" matches a single character, namely any
character that is not matched by the expression obtained by removing
the first '!' from it. (Thus, "[!]a-]" matches any single character
except ']', 'a' and '-'.)

[!...]表示非此范围内的字符;

One can remove the special meaning of '?', '*' and '[' by preceding
them by a backslash, or, in case this is part of a shell command
line, enclosing them in quotes. Between brackets these characters
stand for themselves. Thus, "[[?*\]" matches the four characters
'[', '?', '*' and '\'.

[]中的\,也是一个可用字符;

更多更详细的介绍:http://man7.org/linux/man-pages/man7/glob.7.html

glob模式匹配与和正则表达式的区别

1.元字符代表的意思不同,如glob中*指匹配零个或多个字符,而*在正则表达式中指前面的字符出现零次或多次;

2.regular expressions 支持更多复杂的匹配模式;

3. glob匹配的是整个字符串,而正则表达式匹配的是子字符串,如:

在glob中S*.DOC匹配S.DOC和SA.DOC,但是不匹配POST.DOC或SURREY.DOCKS,但是在正则表达式中匹配的是子字符串,除非用^ 和$ 进行限定^S.*\.DOC$

4. glob用于文件名的匹配,所以通配符不能匹配路径分隔符/,  另外如果文件名是以.开始,则必须准确匹配;

本文链接:https://www.maixj.net/ict/glob-20307

相关文章

留言区

《glob模式匹配》有1条留言

  • 麦新杰

    执行echo *,就理解glob是用来做pathname的匹配的,同时也可以用这个命令来查看glob的开关状态。 []


前一篇:
后一篇:

栏目精选

云上小悟,麦新杰的独立博客

Ctrl+D 收藏本页

栏目


©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1。云上小悟网站部分内容来源于网络,转载目的是为了整合信息,收藏学习,服务大家,有些转载内容也难以判断是否有侵权问题,如果侵犯了您的权益,请及时联系站长,我会立即删除。

网站二维码
go to top