我正在尝试从下面一行中提取 FREE HEAP 计数值,即 1400。这里我使用了两次 split 函数来获取值。有人能帮我获取一个可以使用 split 函数提取值 1400 的正则表达式吗?
my @split_val = split(',',$line);
print $split_val[2];
my @split_val1= split(':',$split_val[2]);
输入字符串:
2014-04-23 14:66:87,666 INFO - HEAP - [USAGE: 1948.3, FREE: 1400, MAX: 3597.5]; NON-HEAP - [USAGE: 611.9, FREE: 52.2, MAX: 1776.0]; CLASSES - [Loaded: 67007, Unloaded: 3237, Left: 63770]; THREADS - [Count: 342]
输出:
1400
答案1
为什么要使用 split?这里 match 更方便:
my $str = '2014-04-23 14:66:87,666 INFO - HEAP - [USAGE: 1948.3, FREE: 1400, MAX: 3597.5]; NON-HEAP - [USAGE: 611.9, FREE: 52.2, MAX: 1776.0]; CLASSES - [Loaded: 67007, Unloaded: 3237, Left: 63770]; THREADS - [Count: 342]';
my ($freeValue) = $str =~ /(?<=FREE: )(\d+(?:\.\d+)?)/;
print $freeValue,"\n";
输出:
1400