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

huangapple go评论84阅读模式
英文:

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

问题

我有一大段文本,我只需要单引号内的内容(不包括单引号)。

例如,这是我正在搜索的一个简化版本。

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

我想返回

a185

到目前为止,我使用的正则表达式是这样的,但它返回了job_id=''以及我需要的数据。我尝试使用捕获组,我以为你可以删除它?

我的正则表达式技能有些陈旧和生疏哈哈 Good regular expression for capturing data inside single quotes but only if it prefixed with something?

(job_id=)'[^']*'

请注意,行中必须包含DEBUG才能匹配所有内容。

英文:

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 Good regular expression for capturing data inside single quotes but only if it prefixed with something?

(job_id=)'[^']*'

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

答案1

得分: 2

你可以使用以下正则表达式来提取信息:

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

并获取第一组的值。可以参考正则表达式演示详细说明

  • DEBUG - 匹配字符串 "DEBUG"
  • .* - 匹配除换行符以外的任意字符,尽可能多地匹配
  • job_id=' - 匹配字符串 "job_id='"
  • ([^']*) - 捕获组 1:匹配除单引号之外的任意字符,可以是零个或多个
  • ' - 匹配单引号字符

可以在Go 在线演示中查看示例代码:

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"
英文:

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"

huangapple
  • 本文由 发表于 2021年11月14日 23:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/69964560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定