1

我想go[^ ]+使用expr. 输出应该是1.17.61.18-becaeea119

go version go1.17.6 linux/amd64
go version devel go1.18-becaeea119 Tue Dec 14 17:43:51 2021 +0000 linux/amd64

但是,这devel部分是可选的,我想不出一种方法来正确地忽略它expr

expr "$(go version)" : ".*go version go\([^ ]*\) .*"
expr "$(go version)" : ".*go version devel go\([^ ]*\) .*"

使用普通的正则表达式,我会这样做,(?: devel)?但由于某种原因expr不支持?

有没有办法expr在一个命令中实现这一点?

4

2 回答 2

0

那是你想要的吗?

.*go version [a-w ]*go\([^ ]*\) .*
于 2022-01-13T19:58:05.013 回答
0

利用

.*go version.* go\([^[:space:]]*\) .*

解释

--------------------------------------------------------------------------------
  .*                       any character (0 or more times)
--------------------------------------------------------------------------------
  go version               'go version'
--------------------------------------------------------------------------------
  .*                       any character (0 or more times)
--------------------------------------------------------------------------------
   go                      ' go'
--------------------------------------------------------------------------------
  \(                       group and capture to \1:
--------------------------------------------------------------------------------
    [^[:space:]]*            any character except: whitespace
                             characters (0 or more times)
--------------------------------------------------------------------------------
  \)                       end of \1
--------------------------------------------------------------------------------
                           ' '
--------------------------------------------------------------------------------
  .*                       any character (0 or more times)
于 2022-01-13T22:59:58.560 回答