[英]git grep <regex containing newline>
I'm trying to grep all line breaks after some binary operators in a project using git bash on a Windows machine.我正在尝试在 Windows 机器上使用 git bash 的项目中的一些二进制运算符之后 grep 所有换行符。
Tried the following commands which did not work:尝试了以下不起作用的命令:
$ git grep "[+-*\|%]\ *\n"
fatal: command line, '[+-*\|%]\ *\n': Invalid range end
$ git grep "[+\-*\|%]\ *\n"
fatal: command line, '[+\-*\|%]\ *\n': Invalid range end
OK, I don't know how to include "-" in a character set, but still after removing it the \n
matches the character n
literally:好的,我不知道如何在字符集中包含“-”,但在删除它之后, \n
仍然与字符n
匹配:
$ git grep "[+*%] *\n"
somefile.py: self[:] = '|' + name + '='
^^^
Escaping the backslash once ( \\n
) has no effect, and escaping it twice ( \\\n
) causes the regex to match \n
(literally).转义一次反斜杠( \\n
)没有效果,转义两次( \\\n
)会导致正则表达式匹配\n
(字面意思)。
What is the correct way to grep here?在这里grep的正确方法是什么?
I don't know how to include "-" in a character set我不知道如何在字符集中包含“-”
There is no need to escape the dash character ( -
) if you want to include it in a character set.如果要将破折号 ( -
) 包含在字符集中,则无需转义它。 If you put it the first or the last character in set it doesn't have its special meaning.如果将它放在 set 中的第一个或最后一个字符,则它没有特殊含义。
Also, there is no need to escape |
还有,没有必要逃避|
inside a character range.在一个字符范围内。 Apart from ^
(when it's the first character in the range), -
(when it is not the first or the last character in the range), ]
and \
(when it is used to escape ]
), all other characters have their literal meaning (ie no special meaning) in a character range.除了^
(当它是范围内的第一个字符时)、 -
(当它不是范围内的第一个或最后一个字符时)、 ]
和\
(当它用于转义]
时),所有其他字符都有它们的字面量字符范围内的含义(即没有特殊含义)。
There is also no need to put \n
in the regexp.也无需将\n
放入正则表达式中。 The grepping tools, by default, try to match the regexp against one row at a time and git grep
does the same.默认情况下,grepping 工具会尝试一次将正则表达式与一行匹配,而git grep
也会这样做。 If you need to match the regexp only at the end of line then put $
(the end of line anchor) as the last character of the regexp.如果您只需要在行尾匹配正则表达式,则将$
(行尾锚)作为正则表达式的最后一个字符。
Your regexp should be [-+*|%] *$
.您的正则表达式应该是[-+*|%] *$
。
Put together, the complete command line is:放在一起,完整的命令行是:
git grep '[-+*|%] *$'
How to find a newline in the middle of a line如何在行中间找到换行符
For lack of better option I think I'll start with:由于缺乏更好的选择,我想我将从:
sudo apt install pcregrep
git grep --cached -Il '' | xargs pcregrep -Mb 'y\nl'
this combines:这结合了:
The output clearly shows the filename and line number, eg:输出清楚地显示了文件名和行号,例如:
myfile.txt:123:my
love
myfile.txt:234:my
life
otherfile.txt:11:my
lion
Tested on Ubuntu 22.04.在 Ubuntu 22.04 上测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.