【问题标题】:Unexpected result while executing terminal command in Jupyter notebook在 Jupyter 笔记本中执行终端命令时出现意外结果
【发布时间】:2021-10-11 07:19:08
【问题描述】:

在 jupyter notebook 中有这个单元格运行正常:

!echo "one two tree"|egrep --color "t[w,r](o|e)"

返回(如预期):一二重e

当这个单元格输入时:

!echo "one two tree"|egrep --color "t[w,r](o|e){1,2}"

在 jupyter 上什么都不返回?!
预期回报:一棵两棵树

我尝试转义花括号

!echo "one two tree"|egrep --color "t[w,r](o|e)\{1,2\}"

返回时还是什么都没有

【问题讨论】:

  • 您使用的是 Mac OS 吗?
  • 是的,在mac终端上它返回OK
  • 试试echo "one two tree"| grep 't[wr][oe]\{1,2\}'。我认为 POSIX BRE 会起作用。
  • 问题似乎出在范围/间隔/限制量词上。 grep -E "t[wr](o|e)(o|e)?" 将工作。另外,看起来大括号需要加倍,试试egrep "t[wr](o|e){{1,2}}"
  • 是的,它成功了! (我标记了你的答案,并用 +1 标记)

标签: shell jupyter-notebook grep


【解决方案1】:

问题是您在字符串中使用了cause variable expansion 的大括号:

bang后面的行可以调用任何安装在底层shell中的程序,支持$variable{variable}形式的变量扩展。

因此,要制作大括号文字,您需要将它们加倍:

egrep "t[wr](o|e){{1,2}}"

【讨论】:

    猜你喜欢
    • 2016-11-24
    • 2016-12-06
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多