你可以使用正则表达式来提取这个子字符串。

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

How can I extract this substring with a regexp?

问题

我想提取在“cache”之后的数字(可以是任何非负整数),但不包括在“total_cache”之后的数字。我需要能够在一个正则表达式中完成这个操作,并且不能使用前瞻或后顾(我正在使用Go语言,它似乎与这里的测试器基本兼容:http://regexpal.com/)。


cache 5764452352
rss 2929250304
rss_huge 0
mapped_file 283906048
pgpgin 19709097
pgpgout 17586611
pgfault 39612525
pgmajfault 3757
inactive_anon 160579584
active_anon 3931484160
inactive_file 3560427520
active_file 1040818176
unevictable 49152
hierarchical_memory_limit 9223372036854775807
total_cache 5764452352
total_rss 2929250304
total_rss_huge 0
total_mapped_file 283906048
total_pgpgin 19709097
total_pgpgout 17586611
total_pgfault 39612525
total_pgmajfault 3757
total_inactive_anon 160579584
total_active_anon 3931484160
total_inactive_file 3560427520
total_active_file 1040818176
total_unevictable 49152
英文:

I want to extract the number (which could be any nonnegative integer) that comes after "cache", but not the one that comes after "total_cache". I need to be able to do this in a single regular expression, and I can't use lookahead or lookbehind. (I'm doing this in go, which appears to be largely compatible with the tester here: http://regexpal.com/)


cache 5764452352
rss 2929250304
rss_huge 0
mapped_file 283906048
pgpgin 19709097
pgpgout 17586611
pgfault 39612525
pgmajfault 3757
inactive_anon 160579584
active_anon 3931484160
inactive_file 3560427520
active_file 1040818176
unevictable 49152
hierarchical_memory_limit 9223372036854775807
total_cache 5764452352
total_rss 2929250304
total_rss_huge 0
total_mapped_file 283906048
total_pgpgin 19709097
total_pgpgout 17586611
total_pgfault 39612525
total_pgmajfault 3757
total_inactive_anon 160579584
total_active_anon 3931484160
total_inactive_file 3560427520
total_active_file 1040818176
total_unevictable 49152

答案1

得分: 5

你需要使用FindStringSubmatch函数,然后提取m[1]。

http://play.golang.org/p/zOixuvDWsi

英文:

You need to use FindStringSubmatch and then extract m[1].

http://play.golang.org/p/zOixuvDWsi

答案2

得分: 2

这应该可以解决:

^cache\s(\d+)

工作中的正则表达式示例:

http://regex101.com/r/jS6sF6

在Go编程语言中,我认为你可以通过使用(?m)来强制多行模式。像这样:

(?m)^cache\s(\d+)

**注意:**我对Go语法不熟悉,所以如果有错误,请原谅我,但是正则表达式是正确的,只需要与多行标志一起使用。

英文:

This should do it:

^cache\s(\d+)

Working regex example:

http://regex101.com/r/jS6sF6

In the Go programming language, I think you can force multi-line mode by using: (?m)
Like so:

(?m)^cache\s(\d+)

Note: Im not familiar with Go syntax, so please forgive me is this is wrong, but the regex is correct, it just needs to be used with the multi-line flag.

答案3

得分: 1

试试这个 -

^cache[ ](\d+)$

使用多行(m)标志(在正则表达式测试器中选中选项 - ^$ 匹配换行符)

英文:

Try this -

^cache[ ](\d+)$

with multi-line(m) flag (In Regex Tester tick the option - ^$ match at line breaks)

huangapple
  • 本文由 发表于 2014年3月11日 04:15:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/22310359.html
匿名

发表评论

匿名网友

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

确定