Using regex in Grep for Windows command line

梦想的初衷 提交于 2021-02-05 10:02:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!