【发布时间】:2015-01-06 10:03:32
【问题描述】:
我正在编写一个脚本,它逐行读取多个文件,检查每一行的文件引用,然后检查这些文件是否存在。
在文件中我有这样的字符串:
example 1: soundSet = { name = "bus_modern", horn = "vehicle/truck_modern/horn.wav" }
example 2: id = "vehicle/bus/citaro/lod_0_w2.msh",
example 3: "vehicle/bus/berkhof_duvedec/berkhof_duvedec_lod_0_w2.msh", "vehicle/bus/berkhof_duvedec/berkhof_duvedec_lod_0_w3.msh",
所以我需要以某种方式从字符串中提取文件名。 我目前的尝试是从字符串中删除所有空格,剪切包括“-char”在内的第一部分,并在第二个“-char”之后切掉所有内容。
这显然不适用于示例 1 和 3: 在示例 1 中,我在字符串的第二部分中获得了文件引用,在示例 3 中,我有两个文件要提取。
不,我很困惑,如何从给定的字符串中提取任何文件引用?
open $filehandle, "<", $file or die "can't open $file\n";
# read the whole file and check for references
while (<$filehandle>)
{
my $line=$_;
my $count=0;
$count++ while ($line =~ m/\//g);
# looks like we found a file-reference
if ( $count > 1)
{
# remove all whitespace now
# prefix whitespace
$line =~ s/^\s+//;
# suffix whitespace
$line =~ ~ s/\s+$//;
# intermediate whitespace
$line=~ s/ //g;
# cut until "
$line=~ s/[^\"]*\"//;
pdebug (2, " rem-pre-\": $line \n");
# chop off all chars after "
my $oper = index($line, '"');
my $word = substr($line, 0, $oper);
$line=$word;
# putting it together
my $searchfile=buildpath($line);
if ( -e $searchfile )
{
pdebug(1,"found\n");
}
else
{
pdebug(1,"not found\n");
print "\nunmatched reference in file:\n$file\n";
findline($file,$line);
print"\ncouldn't find file:\n $searchfile\nreferenced as:\n$line\n";
}
}
到目前为止,这是我的代码的相关部分。未显示的是我正在遍历目录结构以识别必须检查的每个文件的部分。 未在此处显示的代码中使用的潜艇:
pdebug:打印出调试文本
findline:需要文件名和要搜索的字符串,打印出找到它的行号
buildpath:每个文件类型都属于一个子目录(即音频/效果中的 .wav,纹理中的 .tga),buildpath 检查文件名并返回完整路径
有人可以让我在这里找到正确的方向吗?
【问题讨论】:
标签: regex string perl filenames