Note that 111\s(.*)$
matches 111
anywhere inside the string (the first occurrence) and then captures into Submatch 1 any 0+ characters up to the end of the string.
If there is a space before the last sam
, you may use
^111.*\s(\S+)$

Pattern explanation:
^
- start of string
111
- a literal substring 111
.*
- any characters, 0 or more, as many as possible up to the last...
\s
- whitespace
(\S+)
- Submatch 1 capturing one or more non-whitespace characters
$
- end of string.
If you want to get the line that starts with 111
(and any leading whitespace is allowed) and has some whitespace after which your submatch is located, you may consider either
(?m)^\s*111[^\r\n]*\s(\S+)$
(a .
is replaced with [^\r\n]
because in Go regex, a dot .
matches any character incl. a newline), or - to make sure you only match horizontal whitespace:
(?m)^[^\S\r\n]*111[^\r\n]*[^\S\r\n](\S+)$
or even
(?m)^[^\S\r\n]*111[^\r\n]*[^\S\r\n](\S+)$
Explanation:
(?m)^
- start of the line (due to the (?m)
MULTILINE modifier, the ^
now matches a line start and $
will match the line end)
[^\S\r\n]*
- zero or more whitespaces except LF and CR (=horizontal whitespace)
111
- a literal 111
[^\r\n]*
- any 0+ characters other than CR and LF as many as possible up to the last....
[^\S\r\n]
- horizontal space
(\S+)
- Submatch 1 capturing 1+ non-whitespace chars
$
- end of line (prepend with [^\S\r\n]*
or [^\S\n]*
to allow trailing horizontal whitespace)
Here is a Go demo:
package main
import (
"fmt"
"regexp"
)
func main() {
s := `Read SMART Log Directory failed.
111 a bcd sam
111 sam
SMART Error Log not supported`
rex := regexp.MustCompile(`(?m)^[^\S\r\n]*111[^\r\n]*[^\S\r\n](\S+)$`)
fmt.Printf("%#v\n", rex.FindAllStringSubmatch(s,-1))
}
Output: [][]string{[]string{" 111 a bcd sam", "sam"}, []string{" 111 sam", "sam"}}
111
?111
should not be checked, then\S+$
should suffice.func Index(s, sep string) int
+func Split(s, sep string) []string
can yield the expected result.