我正在写一个脚本,它逐行读取几个文件,检查每一行的文件引用,然后检查这些文件是否存在。
在文件中我有这样的字符串:
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:打印出debugtext
findline:期望文件名和字符串搜索,打印出找到它的亚麻布
buildpath:每个文件类型属于一个子目录(即音频/效果中的.wav,纹理中的.tga),buildpath检查文件名并返回完整路径
有人能让我在这里找到正确的方向吗?
答案 0 :(得分:0)
我认为只有一个正则表达式会更好:
while (<$filehandle>)
{
my @filenames = /(?<=")(?:\w+\/)*\w+[.]\w+(?:")/g;
say join("$_\n", @filename) if @filename > 0;
}
请参阅此处获取正则表达式: https://regex101.com/r/bA4oZ7/1
&#39; x&#39; modificator允许编写多行正则表达式并在此处添加一些注释正则表达式的解释(可在此处获取:https://regex101.com/r/bA4oZ7/3)
my $re qr/(?<=") # A filename is after a double quote char
(?:[^"\/]+\/)+ # the path is one or more words separated with a slash
[^"\/]+ # The filename (not the path) can be anything but a double quote char and a slash
[.]\w+ # The extension cannot have space and have the form of .foobar
(?:") # A filename end with a double quote char
/gx;
...
my @filenames = /$re/g;