dourang8305 2019-08-19 10:02
浏览 563
已采纳

如何在Go中检测单个字符串中的正则表达式的所有匹配项

I am attempting to build a regex to detect a unix timestamp like pattern in a string. However, some strings contain multiple "unix time" like patterns and go's regex only detects the first instance of such pattern.

My current regex:

utcRegex, _ := regexp.Compile(^.*\[(\d{7,})\].*)

utcCheck := utcRegex.FindStringSubmatch(string)

utc := utcCheck[1]

Here are some example strings:

Regex works fine with these type of strings

"Nov 6 11:21:34 [14039] : [1541532094] [DEBUG] FOO BAR"

The regex properly detects the 1541532094

Regex does not fulfull what I want

"08-13 11:46:56.379 24980 24980 D SDK: [1565711216] [DEBUG] [15657110953902503] [FOO BAR ]"

The regex only detects 15657110953902503 but not 1565711216. I am only interested in 1565711216. The regex only finds 15657110953902503

Is there an update I can make to my go regex that will detect both of these and then select the first/second instance of this pattern?

  • 写回答

1条回答 默认 最新

  • dqcd84732 2019-08-19 10:08
    关注

    Your regex is too rigid, try:

    \[(\d{7,})\]
    

    and $1 will contain the matches.

    https://regex101.com/r/XoEx56/1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部