简体   繁体   中英

Using regex in Grep for Windows command line

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 ( , ie the group was made capturing since non-capturing ones are not supported.

See proof it is working:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM