Regex 带有fswatch的正则表达式-排除不以“quot;结尾的文件;。txt";

Regex 带有fswatch的正则表达式-排除不以“quot;结尾的文件;。txt";,regex,bash,fswatch,Regex,Bash,Fswatch,对于文件列表,我想匹配那些没有以.txt结尾的文件。我目前正在使用以下表达式: .*(txt$)|(html\.txt$) 应匹配: happiness.html joy.png fear.src madness.html.txt excitement.txt 应该不匹配: happiness.html joy.png fear.src madness.html.txt excitement.txt 我想得到这个,这样我就可以将它与: 问题是它似乎不起作用 PS:我使用TagBa

对于文件列表,我想匹配那些没有以
.txt
结尾的文件。我目前正在使用以下表达式:

.*(txt$)|(html\.txt$)


应匹配:

happiness.html
joy.png
fear.src
madness.html.txt
excitement.txt
应该匹配:

happiness.html
joy.png
fear.src
madness.html.txt
excitement.txt

我想得到这个,这样我就可以将它与:

问题是它似乎不起作用


PS:我使用TagBash而不是fswatch,因为我没有足够的信誉点来创建它。对不起

尝试使用lookback,如下所示:

.*$(?<!\.txt)
*$(?


基本上,只要最后4个字符不是
“.txt”

,就可以匹配任何一行文本

^(?!.*\.txt).+$

您可以使用选项
-p
将此表达式与grep一起使用:

grep -Po '^(?!.*\.txt).+$' file

由于问题已标记为
bash
,因此可能不支持lookaheads(除了
grep-p
),下面是一个不需要lookaheads的
grep
解决方案:

grep -v '\.txt$' file
happiness.html
joy.png
fear.src

编辑:您可以使用此
xargs
命令避免匹配
*.txt
文件:

xargs -0 -n 1 -I {} bash -c '[[ "{}" == *".txt" ]] && echo "{} has been changed"'

这实际上取决于您使用的正则表达式工具。许多工具提供了一种反转正则表达式含义的方法。例如:

猛击 格雷普 对于喜欢冒险的人,posix ERE可以在任何posix兼容的正则表达式引擎中工作

你是在BASH中这样做的吗?我已经更新了我的问题。我想用它作为fswatch的参数。我更新了我的问题以添加更多的上下文。我想用它作为fswatch的参数。非常感谢,它似乎起作用了(尽管我已经将!=改为==所以它只匹配txt文件)确保只匹配
.txt
使用
==
(已编辑)。很高兴它成功了。
# succeeds if filename ends with .txt
egrep -q "\.txt$" <<<"$filename"
# succeeds if filename does not end with .txt
egrep -qv "\.txt$" <<<"$filename"
/\.txt$/ { print "line ends with .txt" }
! /\.txt$/ { print "line doesn't end with .txt" }
$1 ~ /\.txt$/ { print "first field ends with .txt" }
$1 !~ /\.txt$/ { print "first field doesn't end with .txt" }
/[^t]$|[^x]t$|[^t]xt$|[^.]txt$/