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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号