doumi9618 2014-12-21 19:47
浏览 19
已采纳

转到正则表达式以将标签与括号匹配

I want to get the index of all tags inside brackets using regex package.

str := "[tag=blue]Hello [tag2=red,tag3=blue]Good"
rg := regexp.MustCompile(`(?:^|\W)\[([\w-]+)=([\w-]+)\]`)
rgi := fmtRegex.FindAllStringIndex(str, -1)
fmt.Println(rgi)
// Want index for:
// [tag=blue], [tag2=red,tag3=blue]

The regex needs to return indexes for [tag=blue], [tag2=red,tag3=blue]

but it only returns [tag=blue].

How do I fix this regex (?:^|\W)\[([\w-]+)=([\w-]+)\] so that I can also match the comman when there is more than one tags in the brackets

  • 写回答

3条回答 默认 最新

  • doxn43207 2014-12-21 19:51
    关注

    Seems like you want something like this,

    (?<!\w)\[([\w-]+)=([\w-]+)(?:,[\w-]+=[\w-]+)*\]
    

    DEMO

    OR

    \B\[([\w-]+)=([\w-]+)(?:,[\w-]+=[\w-]+)*\]
    

    \B matches between two word characters or two non-word characters.

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?