英文:
Go regex to capture the first occurence with special character
问题
如果给定 (TEXT)testest (GOPHER)mytest (TAG)(not_this)
,
我想要匹配括号内的字符的第一个出现。
正则表达式的结果应该是 TEXT
、GOPHER
、TAG
,但不包括 not_this
,因为它不是该词组中的第一个出现。而且匹配到的文本应该只包含字母,不包含数字。
regexp.MustCompile(`(?i)\([a-z]+\)`)
// 不起作用
请问我应该如何编写正则表达式来匹配这个要求?谢谢!
英文:
If given (TEXT)testest (GOPHER)mytest (TAG)(not_this)
,
I want to grep only the first occurrence of characters inside parentheses.
The regex results need to be TEXT
, GOPHER
, TAG
, but NOT not_this
because this is not the first occurrence in that word phrase. And the grepped text should be only letters not numbers.
regexp.MustCompile(`(?i)\([a-z0-9_-])+\]`)
// is not working
How do I write the regular expression to grep this? Thank you in advance!
答案1
得分: 2
我认为你要找的正则表达式是:
(?:^|\W)\(([\w-]+)\)
含义:
(?:^|\W) /* 查找但丢弃序列开头或非单词字符 */
\(CONTENTS\) /* 包含在括号中的内容 */
(CONTENTS) /* 选择组 */
[\w-]+ /* 单词字符或-,一次或多次 */
英文:
I think the regex you are looking for is:
(?:^|\W)\(([\w-]+)\)
Meaning:
(?:^|\W) /* find but discard the sequence-start or a non-word character */
\(CONTENTS\) /* Contained in () */
(CONTENTS) /* Selection Group */
[\w-]+ /* word character or -, once or more */
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论