微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式 – 使用grep时的报价?

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

包含参数的命令行在执行之前由shell处理.你可以使用echo来查看shell的作用:
$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使用扩展正则表达式).

原文地址:https://www.jb51.cc/regex/357203.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐