我这样有一个文本文件
"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/