GNU的less实用程序的手册页说明了以下关于搜索:
/pattern
Search forward in the file for the N-th line containing the pattern. N
defaults to 1. The pattern is a regular expression, as recognized by the
regular expression library supplied by your system.
Run Code Online (Sandbox Code Playgroud)
我在各种系统上使用较少:我的个人Ubuntu笔记本电脑,我的CentOS云服务器,在Cygwin下工作等等.我一直想做负面预测和其他花哨的东西,但我不知道什么是正则表达式要使用的语法.我怎么知道的?
这是一个编译时参数.较少的./configure脚本知道with-regex=LIBparam.
这是来自上游包的README的引用:
--with正则表达式= LIB
Specifies the regular expression library used by less for pattern
matching. The default is "auto", which means the configure program
finds a regular expression library automatically. Other values are:
posix Use the POSIX-compatible regcomp.
pcre Use the PCRE library.
regcmp Use the regcmp library.
re_comp Use the re_comp library.
regcomp Use the V8-compatible regcomp.
regcomp-local Use Henry Spencer's V8-compatible regcomp
(source is supplied with less).
Run Code Online (Sandbox Code Playgroud)
所以你需要知道'./configured'的减少程度.我在Debian/Ubuntu上调查了这个.他们使用POSIX正则表达式lib.
我还在寻找一种通过脚本动态检测它的方法...... :)
更新:到目前为止,我唯一能做的就是检测是否少用pcre正则表达式.如果使用--with-regex=pcre它配置less ,则链接到libpcre.so共享库:
#!/bin/bash
# ldd prints out the shared libraries a binary is linked to.
# This can be used to check if less is linked against libpcre
if ldd "$(which less)" | grep 'libpcre\.so' ; then
echo "less uses pcre regex syntax"
else
echo "less uses non pcre regex syntax"
# ... more checks should follow. currently trying to find a way
fi
Run Code Online (Sandbox Code Playgroud)
我不知道这是否适用于所有情况(旧版本/不同系统)但我能够使用less --version以下方法找到此信息:
less 458 (GNU regular expressions)
Copyright (C) 1984-2012 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
Run Code Online (Sandbox Code Playgroud)
所以这是GNU正则表达式语法...
在--with-regex=pcre我编译了一个更新的版本之后
less 481 (PCRE regular expressions)
...
Run Code Online (Sandbox Code Playgroud)
更新
感谢crw检查.此解决方案似乎确实是特定于版本的.在greenwoodsoftware(在Linux中)编译可用的源代码后,我发现它不适用于版本436(2009年7月25日发布)和更早版本.它开始工作至少451(2012年9月4日发布)和更晚.(这些之间的版本无法下载).