繁体   English   中英

使用正则表达式捕获具有特殊字符的首次出现

[英]Go regex to capture the first occurence with special character

如果给定(TEXT)testest (GOPHER)mytest (TAG)(not_this)

我只想grep括号内第一次出现的字符。

正则表达式结果必须是TEXTGOPHERTAG ,但不是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM