I have a string and I split the string with regexp package. However I could not create a regexp I need.
The regexp code lines:
v = "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5.result.result[0].code.xTrim()"
r, _ := regexp.Compile(`(@[a-zA-Z0-9] \$[a-zA-Z0-9] )(.*)`)
variableParts := r.FindStringSubmatch(v)
VariablesParts array:
0: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5.result.result[0].code.xTrim()"
1: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5"
2: ".result.result[0].code.xTrim()"
This is my desired result:
Array length can change but array should include this 4 strings.
0: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5.result.result[0].code.xTrim()"
1: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5"
2: ".result.result[0].code.xTrim()"
3: ".xTrim()"
How Can I get this result. Which regexp string I need?
UPDATE for new desired result:
0: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5.result.result[0].code.xTrim()"
1: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5"
2: "result"
3: "result[0]"
4: "code"
5: "xTrim()"
CodePudding user response:
You can replace the last (.*)
part with (.*(\.[^.]*))$
and use
(@[a-zA-Z0-9] \$[a-zA-Z0-9] )(.*(\.[^.]*))$
See the regex demo. Details:
(@[a-zA-Z0-9] \$[a-zA-Z0-9] )
- Group 1: a@
char, then one or more alphanumeric chars, then a#
char and then again one or more alphanumeric chars(.*(\.[^.]*))
- Group 2: any zero or more chars other than line break chars as many as possible, and then Group 3: a.
, zero or more chars other than a.
char$
- at the end of string. Remove if there can be more text on the right.
CodePudding user response:
A better option would be to just use strings.Split:
package main
import (
"fmt"
"strings"
)
func main() {
s := "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5.result.result[0].code.xTrim()"
a := strings.Split(s, ".")
fmt.Println(a)
}