英文:
Golang Split String with Regexp
问题
我有一个字符串,我使用regexp包来分割字符串。然而,我无法创建所需的正则表达式。
正则表达式代码如下:
v := "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5.result.result[0].code.xTrim()"
r, _ := regexp.Compile(`(@[a-zA-Z0-9]+$[a-zA-Z0-9]+)(.*)`)
variableParts := r.FindStringSubmatch(v)
VariablesParts数组:
0: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5.result.result[0].code.xTrim()"
1: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5"
2: ".result.result[0].code.xTrim()"
这是我期望的结果:
数组长度可以改变,但数组应包含以下4个字符串。
0: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5.result.result[0].code.xTrim()"
1: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5"
2: ".result.result[0].code.xTrim()"
3: ".xTrim()"
如何获得这个结果?我需要哪个正则表达式字符串?
更新为新的期望结果:
1: "@636e0e0eac0bd25cd5df92a3$636e0e49ac0bd25cd5df92a5"
2: ".result.result[0].code.xTrim()"
3: ".result"
4: ".result[0]"
5: ".code"
6: ".xTrim()"
英文:
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()"
答案1
得分: 1
你可以将最后的(.*)
部分替换为(.*(\.[^.]*))$
并使用以下正则表达式:
(@[a-zA-Z0-9]+$[a-zA-Z0-9]+)(.*(\.[^.]*))$
详细说明如下:
(@[a-zA-Z0-9]+\$[a-zA-Z0-9]+)
- 第一组:一个@
字符,然后是一个或多个字母数字字符,然后是一个#
字符,再然后是一个或多个字母数字字符。(.*(\.[^.]*))
- 第二组:任意数量的非换行字符(尽可能多),然后是第三组:一个.
字符,然后是零个或多个非.
字符。$
- 字符串末尾。如果右侧可能还有更多文本,请删除此部分。
你可以在正则表达式演示中查看示例。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论