regex - 为什么这个正则表达式不能与 grep 一起使用?

标签 regex macos bash grep

我这样有一个文本文件

"an arbitrary string" = "this is the text one"
"other arbitrary string" = "second text"
"a third arbitrary string" = "the text number three"

我只想获得这个

an arbitrary string
other arbitrary string
a third arbitrary string

即第一个引号内的文本,或第一个 ""= 之间的文本。我使用了这个正则表达式

(?!").*(?=("=))

当我在 RegExr 中尝试时,这是有效的在这个 online tool 。但在我的 OSX 终端中它不起作用,输出为空

grep -o '(?!").*(?=("=))' input.txt

这里出了什么问题?我必须转义某些字符吗?我尝试了所有人,但没有任何改变。

非常感谢您,请原谅我对这个主题缺乏了解。

最佳答案

Lookaheads 和 Lookbehinds 是 PCRE 功能,因此您必须使用参数 -P:

grep -Po '(?!").*(?=(" =))' input.txt

关于regex - 为什么这个正则表达式不能与 grep 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25622967/

相关文章:

regex - sed:在 4GB 单行文件上搜索和替换

mysql - 在 Mac 上安装 MySQL 后如何运行

python - 将 ASCII 字符转换为 "<Uxxx>"unicode 符号的脚本

regex - Powershell 正则表达式检查括号之间的字符串

java - 使用 java.util.regex.Pattern

javascript - 用大括号作为分隔符分割字符串?

python - 无法在 Python 中读取 bash 脚本输出

c# - 我想将我的 Iphone 连接到 MAC mini,并使用 Xamarin UI 测试从我的 Windows 计算机远程运行测试

swift 3 (macOS) : keyDown(with event: NSEvent) doesn't get called

git - 使用 Bash 将空格分隔的文件列表发送到 Git