问题
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|
回答1:
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:
来源:https://stackoverflow.com/questions/61229157/using-regex-in-grep-for-windows-command-line