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组织。openqa。硒。遥远的UnreachableBrowserException如何定义EXE路径?   java Camel AdviceWith不使用指定文件替换端点   基于字符串的java图像加载   Java中的启发式算法,计算8个谜题状态的线性冲突   java为什么不支持文件。probeContentType返回null   JPA@EntityListeners、@PrePersist和Spring@RepositoryEventHandler、@HandleBeforeSave之间的java差异   可能前缀的Java字符串到字符串[]   安装rJava | Makefile时发生java错误。全部:38:target’libjri的配方。所以他失败了   Java公共静态void main()   java如何覆盖txt文件中的某些单词   java如何获得循环中生成的字符值之和?   java Log4j创建另一个具有相同属性的appender   java如何在从Axis2 Web服务客户端通过代理服务器调用Web服务时设置代理设置?   在Windows上安装Elasticsearch时发生java错误   java如何向EditText组件添加TextChangedListener?