bash shell 中的 grep 正则表达式中的空格如何工作?
我读取下面正则表达式的方式:
- 空格字符
- 斜杠字符
- 'n' 字符
- 零个或多个空格字符
- 行尾
但是此测试失败:
$ echo "Some Text \n " | grep " \\n *$"
如果我删除正则表达式中的空格,则不会失败
$ echo "Some Text \n " | grep "\\n *$"
Some Text \n
The way I would read the regular expression below:
- a space char
- a slash char
- a 'n' char
- zero or more space chars
- end of line
But this test fails:
$ echo "Some Text \n " | grep " \\n *$"
If I delete a space in the regular expression, does not fail
$ echo "Some Text \n " | grep "\\n *$"
Some Text \n
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
注意单引号。此 serverfault 问题提供了有关单服务器与单服务器的更多信息。双引号。使用单引号时,字符串按字面意思处理。
这是一个解释。当您执行以下操作时:
echo "Test\n"
,您会得到Test\n
作为输出,因为echo
不会转换转义序列(除非您向其发送-e
标志)。回显“测试\n”| grep '\n'
,它只匹配n
。这是因为\n
是一个“转义的 n”(它似乎没有转换为实际的换行符)。如果您希望它匹配\
和n
,您需要执行echo "Test\n" | grep '\\n'
.Try this:
Note the single quotes. This serverfault question has more information about single vs. double quotes. With single quotes, the string is treated literally.
Here's an explanation. When you do:
echo "Test\n"
, you getTest\n
as the output, becauseecho
doesn't translate the escape sequences (unless you send it the-e
flag).echo "Test\n" | grep '\n'
, it only matches then
. This is because\n
is an "escaped n" (it doesn't seem to translate into an actual newline). If you want it to match the\
and then
, you need to doecho "Test\n" | grep '\\n'
.使用正则表达式时,您必须注意使用它们的上下文。正则表达式引擎以及您用来调用它的机制都会对一些字符进行特殊处理。
在你的情况下,你正在使用 bash。根据您引用内容的方式,您可能必须转义特殊字符两次。一次是为了防止 bash 解释特殊字符,再一次是为了获得您想要的正则表达式行为。
为了解决这样的问题,你应该首先问自己“表达式必须是什么样子?”然后,您还必须问,我必须如何准备该表达式,以便正则表达式引擎真正获得该模式?”这涉及到理解引号对表达式的影响。具体来说,在这种情况下,单引号和双引号之间的区别 (以及其他不太常见的引用机制)。
When using regular expressions you have to be mindful of the context in which you are using them. Several characters are treated specially by the regular expression engine, and also by the mechanism you use to invoke it.
In your case you are using bash. Depending on how you quote things you may have to escape special characters twice. Once to prevent bash from interpreting the special character and once again to get the regex behavior you desire.
To solve problems like this you should first ask yourself "what must the expression look like?" You must then also ask, how must I prepare that expression so that the regular expression engine actually gets that pattern?" This involves understanding the effect that quoting has on the expression. Specifically in this case, the difference between single quote and double quotes (nd the other less comon quoting mechanisms).