oakkose
oakkose

Reputation: 405

Golang Split String with Regexp

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:

1: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5"

2: ".result.result[0].code.xTrim()"

3: ".result"

4: ".result[0]"

5: ".code"

6: ".xTrim()"

Upvotes: 2

Views: 679

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

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.

Upvotes: 1

Related Questions