bash shell 中的 grep 正则表达式中的空格如何工作?

发布于 2024-10-31 17:29:52 字数 298 浏览 0 评论 0原文

我读取下面正则表达式的方式:

  1. 空格字符
  2. 斜杠字符
  3. 'n' 字符
  4. 零个或多个空格字符
  5. 行尾

但是此测试失败:

$ echo "Some Text \n " | grep " \\n *$"

如果我删除正则表达式中的空格,则不会失败

$ echo "Some Text \n " | grep "\\n *$"
Some Text \n

The way I would read the regular expression below:

  1. a space char
  2. a slash char
  3. a 'n' char
  4. zero or more space chars
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九公里浅绿 2024-11-07 17:29:52

试试这个:

echo "Some Text \n " | grep ' \\n *

注意单引号。此 serverfault 问题提供了有关单服务器与单服务器的更多信息。双引号。使用单引号时,字符串按字面意思处理。

这是一个解释。当您执行以下操作时:

echo "Test\n",您会得到 Test\n 作为输出,因为 echo 不会转换转义序列(除非您向其发送 -e 标志)。

回显“测试\n”| grep '\n',它只匹配n。这是因为 \n 是一个“转义的 n”(它似乎没有转换为实际的换行符)。如果您希望它匹配 \n,您需要执行 echo "Test\n" | grep '\\n'.

注意单引号。此 serverfault 问题提供了有关单服务器与单服务器的更多信息。双引号。使用单引号时,字符串按字面意思处理。

这是一个解释。当您执行以下操作时:

echo "Test\n",您会得到 Test\n 作为输出,因为 echo 不会转换转义序列(除非您向其发送 -e 标志)。

回显“测试\n”| grep '\n',它只匹配n。这是因为 \n 是一个“转义的 n”(它似乎没有转换为实际的换行符)。如果您希望它匹配 \n,您需要执行 echo "Test\n" | grep '\\n'.

Try this:

echo "Some Text \n " | grep ' \\n *

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 get Test\n as the output, because echo doesn't translate the escape sequences (unless you send it the -e flag).

echo "Test\n" | grep '\n', it only matches the n. 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 the n, you need to do echo "Test\n" | grep '\\n'.

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 get Test\n as the output, because echo doesn't translate the escape sequences (unless you send it the -e flag).

echo "Test\n" | grep '\n', it only matches the n. 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 the n, you need to do echo "Test\n" | grep '\\n'.

长伴 2024-11-07 17:29:52

使用正则表达式时,您必须注意使用它们的上下文。正则表达式引擎以及您用来调用它的机制都会对一些字符进行特殊处理。

在你的情况下,你正在使用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文