I am trying to write some regular expressions that match and pull apart strings that look like software versions (but they aren't really, so semver parsing, for instance, won't work). I'm having some trouble matching what I am calling "prefix" and "suffix" parts of the input string.
The output I am getting from the following is, strange..
// Sample inputs:
// * '1.2.3-thing' (Prefix: '1.2.3', Suffix: '-thing')
// * '1.2.3+1' (Prefix: '1.2.3', Suffix: '+1')
// * '1.2.3' (Prefix: '1.2.3', Suffix: '')
// * '1' (Prefix: '1', Suffix: '')
// * '1-x' (Prefix: '1', Suffix: '-x')
// * '1-x-x' (Prefix: '1', Suffix: '-x-x')
// * '1.2.3-thing.1' (Prefix: '1.2.3', Suffix: '-thing.1')
// * '1.2-thing-1' (Prefix: '1.2', Suffix: '-thing-1')
// * 'k1.2.3-thing' (Prefix: 'k1.2.3', Suffix: '-thing')
// * 'k-thing-x' (Prefix: 'k', Suffix: '-thing-x')
//
func InspectVersionTag(tag string) {
re := regexp.MustCompile(`^([^\-]+)([\-+].+)$`)
suffix := ""
if re.MatchString(tag) {
tag = re.ReplaceAllString(tag, `$1`)
suffix = re.ReplaceAllString(tag, `$2`)
}
fmt.Println(fmt.Sprintf("Prefix is: %s", tag))
fmt.Println(fmt.Sprintf("Suffix is: %s", suffix))
}
// Current sample output
//
// Input: 1.2.3+1
// Prefix is: 1.2.3
// Suffix is: 1.2.3