【问题标题】:windows grep regex not workingWindows grep 正则表达式不起作用
【发布时间】:2017-03-28 18:10:05
【问题描述】:

Windows 10。

grep 作为 git 安装的一部分安装。

>grep --version

grep (GNU grep) 2.27
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

>git --version

git version 2.12.0.windows.1

使用记事本创建的文件 sample.txt 包含以下 3 行;

a
b
c

获取 grep 以返回以 'a' 或 'c' 开头的行;

>grep ^[ac].*$ sample.txt
a
c

(好的 - 正如预期的那样)

让 grep 返回以任何字符 除了 'a' 或 'c' 开头的行,即以 'b' 开头的行;

>grep ^[^ac].*$ sample.txt
a
c

为什么这不起作用?

【问题讨论】:

  • 命令行使用插入符号表示转义,所以必须自己转义,试试^^ ?
  • 干杯!将其发布为答案,我会接受。 (使用 ^^[^^ ... 虽然 "^[^ ... 也可以使用 - 双引号)。

标签: regex windows git grep


【解决方案1】:

在命令行中,^ 用于指示转义(即,^| 被视为文字 |,而不是命令管道)。

要逃避转义,请加倍:^^[^^ac ...

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多