TransWikia.com

Grab a decimal between 2 strings regex

Stack Overflow Asked by Lenny Gonzalez on November 15, 2021

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!

One Answer

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: Kd+(?:.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: Kd+(?:.d+)+(?= Kubernetes)'

Output

4.3.0

Answered by The fourth bird on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP