regex - 使用grep时引用?

标签 regex grep quotes

Grep的行为会有所不同,具体取决于我在正则表达式中使用哪种引号。我似乎无法清楚地了解为什么会这样。这是问题的示例:

hamiltont$ grep -e show\(  test.txt 
  variable.show();
  variable.show(a);
  variable.show(abc, 132);
  variableshow();
hamiltont$ grep -e "show\("  test.txt 
grep: Unmatched ( or \(
hamiltont$ grep -e 'show\('  test.txt 
grep: Unmatched ( or \(


我只是假设有某种适当的方法将正则表达式用单引号/双引号引起来。有什么帮助吗?

FWIW,grep --version返回grep (GNU grep) 2.5.1

最佳答案

包含自变量的命令行在执行之前由外壳程序处理。您可以使用echo查看外壳程序的作用:

$ echo grep -e show\(  test.txt 
grep -e show( test.txt

$ echo grep -e "show\("  test.txt 
grep -e show\( test.txt

$ echo grep -e 'show\('  test.txt 
grep -e show\( test.txt


因此,不带引号的反斜杠将被删除,从而使“(”成为grep的普通字符(默认情况下,grep使用基本正则表达式,使用-E使grep使用扩展正则表达式)。

关于regex - 使用grep时引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3008423/

相关文章:

mysql - 为什么 MySQL 返回带引号的列名?

javascript - 如何使用 jQuery 将双引号内的破折号 html 元素作为目标?

python - 2 个不同的文本 block 合并在一起。如果我知道 1 是什么,我可以将它们分开吗?

regex - Grep 和正则表达式 : inconsistent behaviour

c# - 缓存模式键的正则表达式 isMatch

Javascript 正则表达式 - 将 * 与多个字符一起使用

bash - 将搜索字符串作为 shell 变量传递给 grep

windows - 等效于 ps -A | Windows 中的 grep -c 进程?

c++ - 在 C++ 中使用特殊格式将字符串拆分为字符串

linux - 测试文件中的每一行是否包含另一个文件中的多个字符串之一