Skip to content

Instantly share code, notes, and snippets.

@jedthe3rd
Last active August 6, 2018 17:06
Show Gist options
  • Save jedthe3rd/4a361cc19ac4cfd4ccf7bf7547107ead to your computer and use it in GitHub Desktop.
Save jedthe3rd/4a361cc19ac4cfd4ccf7bf7547107ead to your computer and use it in GitHub Desktop.
extractObjectInstanceCounterFromQuery regex fix
//Original
//General Counter path pattern is: \\computer\object(parent/instance#index)\counter
//parent/instance#index part is skipped in single instance objects (e.g. Memory): \\computer\object\counter
var counterPathRE = regexp.MustCompile(`.*\\(.*)\\(.*)`)
var objectInstanceRE = regexp.MustCompile(`\\(.*)\\([^()]*)\((.*)\)`)
//extractObjectInstanceCounterFromQuery gets object name, instance name (if available) and counter name from counter path
func extractObjectInstanceCounterFromQuery(query string) (object string, instance string, counter string, err error) {
pathParts := counterPathRE.FindAllStringSubmatch(query, -1)
if pathParts == nil || len(pathParts[0]) != 3 {
err = errors.New("Could not extract counter info from: " + query)
return
}
counter = pathParts[0][2]
//try to get instance name
objectInstanceParts := objectInstanceRE.FindAllStringSubmatch(query, -1)
if objectInstanceParts == nil || len(objectInstanceParts[0]) != 4 {
object = pathParts[0][1]
} else {
object = objectInstanceParts[0][2]
instance = objectInstanceParts[0][3]
}
return
}
//FIX V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//General Counter path pattern is: \\computer\object(parent/instance#index)\counter
//parent/instance#index part is skipped in single instance objects (e.g. Memory): \\computer\object\counter
var counterPathRE = regexp.MustCompile(`.*\\(.*)\\(.*)`)
var objectInstanceRE = regexp.MustCompile(`\\(.*)\\(.*)\((.*)\)`)
func extractObjectInstanceCounterFromQuery(query string) (object string, instance string, counter string, err error) {
pathParts := counterPathRE.FindAllStringSubmatch(query, -1)
if pathParts == nil || len(pathParts[0]) != 3 {
err = errors.New("Could not extract counter info from: " + query)
return
}
counter = pathParts[0][2]
//try to get instance name
objectInstanceParts := objectInstanceRE.FindAllStringSubmatch(query, -1)
if objectInstanceParts == nil || len(objectInstanceParts[0]) != 4 {
object = pathParts[0][2]
} else {
object = objectInstanceParts[0][2]
instance = objectInstanceParts[0][3]
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment