1

How to get the last item by regex?

"Read the information failed.
111 a bcd

SAM Error Log not up supported"

I did this

111\s(.*)$

but it gives me

output = "a bcd sam"

But, I want output of regex which starts with 111 as

output = "sam" // for the line starts with 111

Also, how can i make change if there is any space before 111?

you can test this at https://regex-golang.appspot.com/assets/html/index.html

sam
  • 18,509
  • 24
  • 83
  • 116

3 Answers3

3

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+)$

enter image description here

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"}}

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

This should work for you:

\s(\w+)$ // The output will be `sam`

This means capture the last string ($) after a whitespace.

Endre Simo
  • 11,330
  • 2
  • 40
  • 49
1

You can use this:

^\s*1{3}.*\s(\S+)$

^ start of line

\s* 0 or more occurrences of space in the beginning

1{3} followed by three ones (i.e. 111)

.* followed by anything

\s followed by space

(\S+)$ ending with non-space characters. First capture group.

Maria Ivanova
  • 1,146
  • 10
  • 19