如何在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?

Your regex is too rigid, try:

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

and $1 will contain the matches.

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