开发者

How do spaces work in a regular expression with grep in the bash shell?

开发者 https://www.devze.com 2023-02-25 15:32 出处:网络
The way I would read the regular expression below: a space char a slash char a \'n\' char zero or more space chars

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


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'.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消