在 Windows 命令行的 Grep 中使用正则表达式 [英] Using regex in Grep for Windows command line
问题描述
我想捕获包含恰好 3 个字段的所有行,其中一个字段是任何字符串(可能为空)后跟一个 |
(并且可能有一些最终的行尾的文本).
我设法构建了一个 regex
,它似乎完全符合我的要求
^(?:[^|]*|){3}[^|]*$
当我在
I want to capture all lines which contain exactly 3 fields, where a field is any string (possibly empty) followed by a |
(and there may be some final text at the end of the line).
I managed to build a regex
which seems to do exactly what I want
^(?:[^|]*|){3}[^|]*$
and when I try it on 101regex it seems to work just fine.
However, I am having problems to run this regex
on the Windows command line via grep
and I guess it has something to do with the proper escaping.
I tried
grep -E '^^(?:[^^^^|]*^^|){3}[^^^^|]*$' test.txt
grep -E '^^(?:[^^^|]*^|){3}[^^^|]*$' test.txt
but nothing helped. Any ideas?
Test Input
0|1|2|3
0|1|2|
|1|2|3
|1|2|
|1|2
|1|
0|1|2
0|1|
|1|2|3|4
|1|2|3|
0|1|2|3|4
0|1|2|3|
In grep
, when you use POSIX ERE regex engine, you need to avoid backslashes in bracket expressions and non-capturing groups:
grep -E "^([^|]*|){3}[^|]*$" test.txt
Here, [^|]
is turned into [^|]
(since POSIX bracket expressions do not treat escaped chars as regex escapes) and (?:
is replaced with (
, i.e. the group was made capturing since non-capturing ones are not supported.
See proof it is working:
这篇关于在 Windows 命令行的 Grep 中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!