File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package strutil
22
33import (
44 "errors"
5+ "regexp"
56 "strconv"
67 "strings"
78 "time"
@@ -10,6 +11,13 @@ import (
1011 "github.com/gookit/goutil/byteutil"
1112)
1213
14+ var regNumVersion = regexp .MustCompile (`[0-9][\d.]+([_-]\d+)?` )
15+
16+ // NumVersion parse input string, get valid number version. eg: go-1.22.3 -> 1.22.3
17+ func NumVersion (s string ) string {
18+ return regNumVersion .FindString (s )
19+ }
20+
1321// MustToTime convert date string to time.Time
1422func MustToTime (s string , layouts ... string ) time.Time {
1523 t , err := ToTime (s , layouts ... )
Original file line number Diff line number Diff line change @@ -9,6 +9,16 @@ import (
99 "github.com/gookit/goutil/timex"
1010)
1111
12+ func TestGetVersion (t * testing.T ) {
13+ is := assert .New (t )
14+ is .Eq ("1.2.0" , strutil .NumVersion ("v1.2.0" ))
15+ is .Eq ("3.19.6" , strutil .NumVersion ("flutter-3.19.6" ))
16+ is .Eq ("3.19.6.3" , strutil .NumVersion ("flutter-3.19.6.3" ))
17+ is .Eq ("1.8.0_442" , strutil .NumVersion ("corretto-1.8.0_442" ))
18+ is .Eq ("1.8.0-442" , strutil .NumVersion ("corretto-1.8.0-442" ))
19+ is .Eq ("20.16.0" , strutil .NumVersion ("node-v20.16.0-win-x64" ))
20+ }
21+
1222func TestToTime (t * testing.T ) {
1323 is := assert .New (t )
1424
You can’t perform that action at this time.
0 commit comments