String pattern matching

I have a string,

s := “root 1 12345 /root/mypath/jdk.1.8.0.25/ catalina.org.startup”

I need to grep only the version number using go and save the result in a string. Can somebody please help

Which part is the version number you want - 1, 12345, or 1.8.0.25? If the latter, do you know it will always be preceded by “jdk.“ and be four numbers? If not, what do you know about it?

1 Like

I need only the value “1.8.0.25”

In general think how do you solve in the other languages or programmatically in general. We will end up a dumb version immediately (my version in go is below). Then try to replicate the same in go and use any language specific features later to make it optimised. Now its not a big deal right ?

package main

import (
“fmt”
“strings”
)

func main() {

inputStr:="root 1 12345 /root/mypath/jdk.1.8.0.25/ catalina.org.startup";
startIndex := strings.Index(inputStr,"/j");
endIndex := strings.Index(inputStr,"/ ");
fmt.Println("Start and end indexs are, %s : %s",startIndex,endIndex)
javaVersion :=inputStr[startIndex+1:endIndex] 
fmt.Println("Your java version is",javaVersion)

}

Indeed you have to use a better compare string and error handling. I will leave that for you anyway.
play around here : Go Playground - The Go Programming Language

A quick one-liner https://play.golang.org/p/uYZAOEDe0iQ

This is very quick. It will not work if there is not in the third path element.

So @sneha_surendranathan will still have to answer the question by @calmh:

If the latter, do you know it will always be preceded by “jdk.“ and be four numbers? If not, what do you know about it?

All depends if OP is working with fixed paths or not. It was just meant as an example of how he can extract it out in a quick one-liner.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.