[英]Go regex to capture the first occurence with special character
如果给定(TEXT)testest (GOPHER)mytest (TAG)(not_this)
,
我只想grep括号内第一次出现的字符。
正则表达式结果必须是TEXT
, GOPHER
, TAG
,但不是not_this
因为这不是该单词短语中的第一个匹配项。 grepped的文本应仅是字母,而不是数字。
regexp.MustCompile(`(?i)\([a-z0-9_-])+\]`)
// is not working
我如何编写正则表达式来grep这个? 先感谢您!
我认为您正在寻找的正则表达式是:
(?:^|\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 */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.