Regex 在string README_*和REAME_IMP之间应用正则表达式_*
我使用Regex 在string README_*和REAME_IMP之间应用正则表达式_*,regex,string,colors,background-color,ls,Regex,String,Colors,Background Color,Ls,我使用grc将ls命令的结果着色 目前,我有以下规则在蓝色背景上以白色前景显示文本。我为此付出了: #README regexp=(README_[^.]*$) colours=on_blue blink 现在,我希望以自述文件\u IMP.*开头的字符串(星形*表示任何其他字符串)显示在红色背景的白色前景中 我在同一个文件中: #README_IMP regexp=(README_IMP[^.]*$) colours=on_red blink 但问题是,即使我键入$touch READM
grc
将ls
命令的结果着色
目前,我有以下规则在蓝色背景上以白色前景显示文本。我为此付出了:
#README
regexp=(README_[^.]*$)
colours=on_blue blink
现在,我希望以自述文件\u IMP.*
开头的字符串(星形*
表示任何其他字符串)显示在红色背景的白色前景中
我在同一个文件中:
#README_IMP
regexp=(README_IMP[^.]*$)
colours=on_red blink
但问题是,即使我键入$touch README\u IMP\u any\u string
,文本也会不幸地显示在蓝色背景的白色前景中,也就是说,第一条规则regexp=(README\u[^.]*$)
的优先级在regexp=(README\u IMP[^.]*$)
如何使用适当的regexp(带grc
)区分字符串README\u any\u string
与README\u IMP\u any\u string
PS:您可以查看regexpgrc
engine on的示例
更新1:遵循@Ludovic Kuty
的建议,如果我将两条规则之间的顺序颠倒如下:
#README_IMP
regexp=(README_IMP[^.]*$)
colours=on_red blink
# README
regexp=(README_[^.]*$)
colours=on_blue blink
第二条规则似乎覆盖了第一条规则,如下图所示:
如您所见,文件READ_IMP_1
在蓝色上为白色,而不是预期的红色上为白色
更新2:据我所见,文件conf.ls
是从上到下读取的,我无法解释为什么在我创建README\u字符串时,负regexp=(README\u(?!IMP)[^.]*$)
不匹配,其中字符串是除IMP
之外的任何字符串
例如,在我的conf.ls
中,我暂时设置了:
# README with no IMP after
regexp=(README_(?!IMP)[^.]*$)
colours=on_blue blink
# README_IMP
regexp=(README_IMP[^.]*$)
colours=on_red blink
例如,如果创建一个名为README_的文件,则后者将以“on_红色闪烁”的颜色显示,而另一个带有*
且不包含IMP
字符串的README将以白色显示:
为什么在这种情况下,regexp=(自述文件[^.]*$)
不匹配
也许dircolors
和grc
之间存在冲突
这里是~/.dircolors的开头:
#NORMAL 01;37 # global default, although everything should be something.
FILE 01;37 # normal file
DIR 32 # directory
LINK 01;36 # symbolic link
FIFO 40;33 # pipe
SOCK 01;35 # socket
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
#NORMAL 01;37 # global default, although everything should be something.
FILE 01;37 # normal file
DIR 32 # directory
LINK 01;36 # symbolic link
FIFO 40;33 # pipe
SOCK 01;35 # socket
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
如您所见,我对NORMAL
行进行了注释,认为它可以进行更改,但实际上没有任何后果
看起来RESTEST regexp规则具有优先级,即使匹配错误也会应用该规则
更新3:
鉴于没有人能够提出解决方案,我认为我将不得不修改ls.c并按照前面的文章重新编译。我明白,这就像用核弹炸死一只鸡,但我似乎没有其他选择
更新4:这里是@NechesStich
建议的最后一次尝试:
# README_IMP
regexp=(README_IMP[^.]*$)
colours=on_red blink
# README with no IMP after
regexp=README_[^.I][^.]*$|README_[^.][^.M][^.]*$|README_[^.][^.][^.P][^.]*$|README_I$|README_IM$
colours=on_blue blink
不幸的是,自述文件*
(其中*不以IMP开头)和自述文件*
(带有*任何字符串)之间的区别
我得到以下结果:
正如您所看到的,自述文件是公认的,其中的README\u IMP\u 1
用白色表示:
我认为普通文件的白色与grc的白色之间的冲突来自~/.dircolors:
#NORMAL 01;37 # global default, although everything should be something.
FILE 01;37 # normal file
DIR 32 # directory
LINK 01;36 # symbolic link
FIFO 40;33 # pipe
SOCK 01;35 # socket
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
#NORMAL 01;37 # global default, although everything should be something.
FILE 01;37 # normal file
DIR 32 # directory
LINK 01;36 # symbolic link
FIFO 40;33 # pipe
SOCK 01;35 # socket
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
我不明白为什么dircolors
优先于grc
它值得更多的调查,我将做其他的测试
更新5这里是从grc
文档中摘录的关于字符串着色策略的内容:
**count** is one of words: once, more, stop, previous, block or unblock
- **once** means that if the regexp is matched, its first occurrence is coloured and the program will continue with other regexp's.
- **more** means that if there are multiple matches of the regexp in one line, all of them will be coloured.
- **stop** means that the regexp will be coloured and program will move to the next line (i.e. ignoring other regexp's)
- **previous** means the count will be the same as for the previous line
- **block** marks a start of a multiline block of text, coloured with the same colour
- **unblock**, obviously, marks the end of such a block
example:
# this is probably a pathname
regexp=/[\w/\.]+
colour=green
count=more
this will match `/usr/bin`, `/usr/local/bin/`, `/etc/init.d/syslogd` and similar strings and paint it with green.
Another example:
regexp=^-{1,2}\s{0,1}$
colours=red
count=block
-
regexp=^\s{0,5}$
colours=default
count=unblock
this will turn all correctly formatted mail signatures red.
Regular expressions are evaluated from top to bottom, this allows nested and overlapped expressions. (e.g. you colour everything inside parentheses with one colour, and if a following expression matches the text inside parentheses, it will be also coloured).
在我的例子中,你能看到一个应用程序吗?阅读(第157-165行)上的源代码
<> p>你可以看到GRC没有考虑用“x”或“n”开头的行来启动新的规则,只使用其他非字母字符的行,如果你看其他例子,他们将它们的规则与
这就是为什么只有你的最后一条规则被应用,因为它们没有被正确地分开,它们正在互相覆盖
像这样分开时应该能用
# README with no IMP after
regexp=README_(?!IMP)[^.]*$
colours=on_blue blink
=======
# README_IMP
regexp=README_IMP[^.]*$
colours=on_red blink
regexp=(README\u IMP\u[^.]*$)
是的,这是所有以README\u IMP\u
开头的文件的全部内容,我的意思是README\u IMP\u*
带有*
任何字符串。您可以交换规则的顺序吗?更具体的第一点。@LudovicKuty。你可以在上面我的更新中看到结果。它似乎对结果没有影响,见图。Regard我既无法重现问题,也无法使grc
正常工作。我使用带有两个文件的目录:README\u IMP\u any\u string
和README\u any\u string
。正则表达式在~/.grc/ls.conf
中是您的(只需在红色上和蓝色上即可),测试是ls-l | grcat ls.conf
。我得到的是README\u IMP\u any\u string
的红色背景,而另一个则没有。所以我很难帮助你,因为我无法重现你的问题,甚至无法使它正常工作。它工作了!谢谢!