如果给定 (TEXT)test (GOPHER)mytest (TAG)(not_this)
,
我只想 grep 括号内第一次出现的字符。
正则表达式结果需要是TEXT
、GOPHER
、TAG
,但不是not_this
,因为这不是该单词短语中的第一次出现。并且 grepped 文本应该只是字母而不是数字。
regexp.MustCompile(`(?i)\([a-z0-9_-])+\]`)
// is not working
如何编写正则表达式来 grep this?提前致谢!
最佳答案
我认为 regex您正在寻找的是:
(?:^|\W)\(([\w-]+)\)
含义:
(?:^|\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 */
关于regex - 转到正则表达式以捕获具有特殊字符的第一次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27490455/