dourang8305 2019-08-19 18: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 18:08
    关注

    Your regex is too rigid, try:

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

    and $1 will contain the matches.

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

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog