我想要grep的手册页gcc的选项'-v‘。
man gcc | grep -w '\-v'工作。但我想用regex。
然而,正如我所预期的,在单词的开头,下面的行与'-v‘不匹配:
grep '\<-v' <<<$'-v'为什么?
发布于 2019-05-29 05:51:56
单词边界转义序列与-w选项和man grep略有不同。
-w,-单词-regexp 只选择那些包含构成整个单词的匹配的行。测试是,匹配的子字符串必须位于行的开头,或者前面必须有一个非单词组成字符。类似地,它必须在行尾或后面跟着一个非单词组成字符。单词组成字符是字母、数字和下划线.
然而,regex中的单词边界只有在有单词字符时才会起作用。
$ # this fails because there is no word boundary between space and +
$ # assumes \b is supported, like GNU grep
$ echo '2 +3 = 5' | grep '\b+3\b'
$ # this works as -w only ensures that there are no surrounding word characters
$ echo '2 +3 = 5' | grep -w '+3'
2 +3 = 5
$ # doesn't work as , isn't at start of word boundary
$ echo 'hi, 2 one' | grep '\<, 2\>'
$ # won't match as there are word characters before ,
$ echo 'hi, 2 one' | grep -w ', 2'
$ # works as \b matches both edges and , is at end of word after i
$ echo 'hi, 2 one' | grep '\b, 2\b'
hi, 2 onehttps://stackoverflow.com/questions/56353539
复制相似问题