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

6
  • You want to obtain the last chunk of non-whitespace characters if the string starts with 111? Jul 19, 2016 at 6:30
  • yes true. in this case i wanted "sam". i.e. last element after white space
    – sam
    Jul 19, 2016 at 6:31
  • If 111 should not be checked, then \S+$ should suffice. Jul 19, 2016 at 6:31
  • but the string starting with 111
    – sam
    Jul 19, 2016 at 6:32
  • Isn't a non-regex way easier? func Index(s, sep string) int + func Split(s, sep string) []string can yield the expected result. Jul 19, 2016 at 6:35

3 Answers 3

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

5
  • yes but it should work for the line starts with 111.
    – sam
    Jul 19, 2016 at 6:39
  • 1
    No problem, it is the same, just add it to the pattern. The point is the \s. It is what makes this pattern work. Jul 19, 2016 at 6:41
  • thanks very much. I have updated the string little. can you explain me hw it will work for that string?
    – sam
    Jul 19, 2016 at 6:43
  • Your string is multiline. Either you split the string into an array of trimmed lines and use the regex above, or use (?m)^\s*111.*\s(\S+)$ Jul 19, 2016 at 6:44
  • 1
    I think (?m)^[^\S\r\n]*111[^\r\n]*[^\S\r\n](\S+)$ is safer to use with a multiline string. Jul 19, 2016 at 6:48
1

This should work for you:

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

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

2
  • it must start with 111
    – sam
    Jul 19, 2016 at 6:34
  • can you show a concrete example with the desired result you want to obtain?
    – Endre Simo
    Jul 19, 2016 at 6:35
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.