1

I'm still pretty new to regex and I have this string:

Client Version: openshift-clients-4.3.0-201910250623-88-g6a937dfe Server Version: 4.3.0 Kubernetes Version: v1.16.2:q

And I wanted to grab 4.3.0, which is between Server Version: and Kubernetes

I thought I could do something like: (\d*\.?\d+\.\d), which grabs every decimal number that has a length of 3, but I just want it to return a single number.

So I tried (Server Version: )+(\d*\.?\d+\.\d) but this gives me 2 capture groups and I want to store the number 4.3.0 in a variable I'm playing around here

Wanted to use this regex with grep or sed

Any help is appreciated!

Lenny Gonzalez
  • 123
  • 1
  • 1
  • 8

1 Answers1

2

Use an outer capturing group, and repeat the dot and the digits inside the group using another group if you want to use sed.

Server Version: (\d+(\.\d+)+) Kubernetes

Regex demo | Sed demo

If you can use grep -P you could only get the match.

Server Version: \K\d+(?:\.\d+)+(?= Kubernetes)

Regex demo | Bash demo

For example

echo "Client Version: openshift-clients-4.3.0-201910250623-88-g6a937dfe Server Version: 4.3.0 Kubernetes Version: v1.16.2:q" | grep -oP 'Server Version: \K\d+(?:\.\d+)+(?= Kubernetes)'

Output

4.3.0
The fourth bird
  • 154,723
  • 16
  • 55
  • 70