在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屋!