我想從字符串中提取大小值。如何在使用Perl的正則表達式中進行可選匹配?
數據尺寸:(2000個字節)
或
文件數據大小:(2082個字節)
該字符串可以以兩種方式中的一種進行格式化
如果字符串存在於文件中,則它只會出現一次。
到目前爲止,我有:
#!/usr/bin/perl
use strict;
use warnings;
open FILE, "</tmp/test";
my $input = do { local $/; <FILE> };
my ($length) = $input =~ /(file)?\s*Data-Size: \((\d+) bytes\)/m;
$length or die "could not get data length\n";
print "length: $length\n";
這個問題似乎是與製作word文件可選。我想我可以這樣做:
(file)?
但是,這似乎是停止匹配時,字文件不存在。另外,當字文件在那裏時,它將字符串「file」設置爲$ length。我認爲這是因爲圍繞文件的括號也意味着提取。
那麼如何匹配兩個字符串中的任何一個並提取大小值?
工作得很好 - 感謝。 – FunLovinCoder