dqy13020940 2014-12-15 18:17
浏览 92
已采纳

使用正则表达式捕获具有特殊字符的第一个匹配项

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条回答 默认 最新

  • douzi7890 2014-12-15 19:02
    关注

    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 */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波