glob匹配库,提供类似于“re”模块的接口。

globre的Python项目详细描述


使用apache将glob匹配模式转换为正则表达式 茧式规则(有一些扩展)。

tl;dr

安装:

$ pip install globre

使用:

importglobrenames=['/path/to/file.txt','/path/to/config.ini','/path/to/subdir/base.ini',]txt_names=[namefornameinnamesifglobre.match('/path/to/*.txt',name)]asserttxt_names==['/path/to/file.txt']ini_names=[namefornameinnamesifglobre.match('/path/to/*.ini',name)]assertini_names==['/path/to/config.ini']all_ini_names=[namefornameinnamesifglobre.match('/path/to/**.ini',name)]assertall_ini_names==['/path/to/config.ini','/path/to/subdir/base.ini']

详细信息

这个包基本上允许使用类unix shell的文件名globbing 用于匹配python程序中的字符串。全球匹配 允许大多数字符与自身匹配,如下所示 具有特殊含义的序列:

SequenceMeaning
^{tt1}$Matches any single character except the slash (‘/’) character.
^{tt2}$Matches zero or more characters excluding the slash (‘/’) character, e.g. ^{tt3}$ which will not match “/etc/foo/bar.conf”.
^{tt4}$Matches zero or more characters including the slash (‘/’) character, e.g. ^{tt5}$ which will match “/lib/foo/bar.so”.
^{tt6}$Escape character used to precede any of the other special characters (in order to match them literally), e.g. ^{tt7}$ will match “foo” followed by a literal question mark.
^{tt8}$Matches any character in the specified regex-style character range, e.g. ^{tt9}$.
^{tt10}$Inlines a regex expression, e.g. ^{tt11}$ which will match “foo-bar.txt” but not “foo-012.txt”.

globre包导出以下函数:

  • globre.match(pattern, string, sep=None, flags=0)

    测试glob模式是否与字符串匹配。如果它 则返回re.matchobject,否则返回None。字符串 必须完全匹配。有关 sep标志参数。示例:

    globre.match('/etc/**.conf','/etc/rsyslog.conf')# => truthy
  • globre.search(pattern, string, sep=None, flags=0)

    类似于globre.match,但模式不需要匹配 整个绳子。示例:

    globre.search('lib/**.so','/var/lib/python/readline.so.6.2')# => truthy
  • globre.compile(pattern, sep=None, flags=0, split_prefix=False)

    将指定的模式编译为具有 与re.compile返回的正则表达式对象相同的api。

    sep参数指定分层路径组件 要使用的分隔符。默认情况下,它使用unix风格的正斜杠 分隔符("/"),但可以重写为 可选的有效分层路径组件分隔符。 注意,尽管sep可以设置为向前和向后- 斜杠(即"/\\")理论上支持Unix和 Windows样式的路径组件,这有一个明显的缺陷 然后两个字符可以在与 分隔符。

    位掩码可以包含所有标准的 添加到globre.EXACT标志。如果精确设置,则 返回的正则表达式将包含与前导“^”和 尾随“$”,这意味着正则表达式必须与整个字符串匹配, 从头到尾。

    如果split_prefix是truthy,则返回值变为 第一个元素设置为在中找到的任何初始非通配符字符串 模式。第二个元素仍然是regex对象。 例如,模式foo/**.ini将生成一个元组 相当于('foo/',re.compile('foo/.*\\.ini'))

    示例:

    prefix,expr=globre.compile('/path/to**.ini',split_prefix=True)# prefix => '/path/to'names=['/path/to/file.txt','/path/to/config.ini','/path/to/subdir/otherfile.txt','/path/to/subdir/base.ini',]fornameinnames:ifnotexpr.match(name):# ignore the two ".txt" filescontinue# and do something with:#   - /path/to/config.ini#   - /path/to/subdir/base.ini

那么glob模块呢

这个包不同于 以下关键方法:

  • glob模块在实际的文件系统上运行;globre可以是 用于匹配文件系统上的两个文件以及任何其他文件 要匹配的字符串源。
  • glob模块不提供**“降序”匹配器。
  • glob模块不提供{...}正则表达式 内联特性。
  • glob模块不提供备用层次结构分隔符 超过/\\

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何序列化数字?   java如何用我的Mainclass扩展ListActivity类和Fragment类?   JavaJersey、Jackson和JAXRS发布了多种JSON格式   java如何使用JavaFXKeyCombination覆盖系统默认的键盘快捷键,如Ctrl+C、Ctrl+V?   java Jersey类路径扫描示例Jersey。配置。服务器供应商。类路径   java什么样的数据结构可以作为一个数组,但在同一索引下给定多个值时会自动增长到第二维度?   java如何序列化非持久实体中的嵌套PersistentEntityResource   协议缓冲区我可以让protoc在Java中生成int数组吗?   在GregorianCalendar ArrayList Java中添加日期元素   从html模板动态生成pdf文件并用java生成目录   java Gridgain 6.5.5开源多个节点速度较慢。。?   java如何检查数组中所有整数的不相等性?   java在Eclipse中,如何多次运行JUnit测试用例   java侦听器不能处理特定的片段   java不是一个声明?(蓝色J)   找不到Attributer类型的PersonId的java定义