英文:
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
答案2
得分: 2
这应该可以解决:
^cache\s(\d+)
工作中的正则表达式示例:
在Go编程语言中,我认为你可以通过使用(?m)
来强制多行模式。像这样:
(?m)^cache\s(\d+)
**注意:**我对Go语法不熟悉,所以如果有错误,请原谅我,但是正则表达式是正确的,只需要与多行标志一起使用。
英文:
This should do it:
^cache\s(\d+)
Working regex example:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论