Search code examples
regexgoregex-group

Good regular expression for capturing data inside single quotes but only if it prefixed with something?


I have a massive amount of text I just need the contents of whats inside the single quotes (excluding the single quotes).

for example, here's a cutdown version of what I am searching.

output line from channel: [2021-11-14 15:59:20] config='954'!
output line from channel: [2021-11-14 15:59:21] DEBUG: job_name='test' disabled=true
output line from channel: [2021-11-14 15:59:25] DEBUG: job_id='a185' configsized

and I would like to return

a185

The regular expression I have so far is this, but it returns the jobid='' - as well as the data i required. I tried to use a capture group and I thought you could delete it?

My regex skills are old and out of touch lol :-)

(job_id=)'[^']*'

Note that the line has to have DEBUG on it somewhere to match everything.


Solution

  • You can use

    DEBUG.*job_id='([^']*)'
    

    and get the Group 1 value. See the regex demo. Details:

    • DEBUG - a DEBUG string
    • .* - any zero or more chars other than line break chars, as many as possible
    • job_id=' - a job_id=' string
    • ([^']*) - Capturing group 1: any zero or more chars other than '
    • ' - a ' char.

    See the Go demo online:

    package main
    
    import (
        "fmt"
        "regexp"
    )
    
    func main() {
        markdownRegex := regexp.MustCompile(`DEBUG.*job_id='([^']*)'`)
        results := markdownRegex.FindStringSubmatch(`output line from channel: [2021-11-14 15:59:25] DEBUG: job_id='a185' configsized`)
        fmt.Printf("%q", results[1])
    }
    // => "a185"