我有一個 Perl 程式:
- 讀取用 C 撰寫的 SRC 檔案
- 使用 SRC 檔案中的正則運算式匹配來查找特定格式的資料以用作目標檔案名
- 打開新的目標檔案
- 執行另一個正則運算式匹配以查找所有包含關鍵字 abcd 的 C 樣式注釋 /* */。注意:這些注釋可以是 1 行或多于 1 行,因此正則運算式會查找第一個 /*,然后是關鍵字 abcd,然后是任意數量的文本和空格,然后才遇到結束 */
- 將正則運算式匹配寫入目標檔案
#!/usr/bin/perl
use warnings;
use strict;
my $src = 'D:\\Scripts\\sample.c';
my $fileName;
# open source file for reading
open(SRC_FH,'<',$src) or die $!;
while(my $row = <SRC_FH>){
if ($row =~ /([0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{2}|[0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{3})/){
$fileName = $1;
}
}
my $des = "D:\\Scripts\\" . $fileName . ".txt";
# open destination file for writing
open(DES_FH,'>',$des) or die $!;
print("copying content from $src to $des\n");
seek SRC_FH, 0, 0;
while(my $row = <SRC_FH>){
if ($row =~ /(\/\*.*abcd.[\s\S]*?\*\/)/){
print DES_FH "$1\n";
}
}
# always close the filehandles
close(SRC_FH);
close(DES_FH);
print "File content copied successfully!\n";
我的問題是我認為由于 perl 代碼的執行方式雖然通過正則運算式是正確的,但我的目標檔案只得到寫入它的 1 行注釋。任何超過 1 行的 C 樣式注釋都不會寫入目標檔案。我的第二個 if 陳述句中缺少什么?
我在這里檢查了我的第二個 if 陳述句正則運算式https://regexr.com/它的作業原理是捕獲多行 C 樣式注釋以及還包含關鍵字 abcd 的單行注釋。
所以我嘗試了 zdim 下面的第一個建議。這是我使用的:
#!/usr/bin/perl
use warnings;
use strict;
my $src = 'D:\\Scripts\\sample.c';
my $fileName;
my @comments;
# open source file for reading
open(SRC_FH,'<',$src) or die $!;
while(my $row = <SRC_FH>){
if ($row =~ /([0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{2}|[0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{3})/){
$fileName = $1;
}
}
my $des = "D:\\Scripts\\" . $fileName . ".txt";
# open destination file for writing
open(DES_FH,'>',$des) or die $!;
print("copying content from $src to $des\n");
#seek SRC_FH, 0, 0;
my $content = do {
#read whole file at once
local $/;
open (SRC_FH,'<', $src) or die $!;
<SRC_FH>;
};
#if($content =~ /(\/\*.*abcd.[\s\S]*?\*\/)/sg){
# my @comments = $content;
# }
my @comments = $content =~ /(\/\*.*abcd.[\s\S]*?\*\/)/sg;
foreach (@comments){
print DES_FH "$1\n";
}
#while(my $row = <SRC_FH>){
# if ($row =~ /(\/\*.*abcd.[\s\S]*?\*\/)/){
# print DES_FH "$1\n";
# }
#}
# always close the filehandles
close(SRC_FH);
close(DES_FH);
print "File content copied successfully!\n";
結果是將 sample.c 中的所有內容復制到目標檔案中。完整的 1:1 副本。我希望從 C 檔案中提取所有單行和多行注釋。
示例 1:/* abcd */ 示例 2:/* 一些文本 * 更多評論 abcd 和更多評論 */
最終解決方案
#!/usr/bin/perl
use warnings;
use strict;
my $src = 'D:\\Scripts\\sample.c';
my $fileName;
# open source file for reading
open(SRC_FH,'<',$src) or die $!;
while(my $row = <SRC_FH>){
if ($row =~ /([0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{2}|[0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{3})/){
$fileName = $1;
}
}
my $des = "D:\\Scripts\\" . $fileName . ".txt";
# open destination file for writing
open(DES_FH,'>',$des) or die $!;
print("copying content from $src to $des\n");
seek SRC_FH, 0, 0;
my $content = do{local $/; <SRC_FH>};
my @comments = $content =~ /(\/\*.*abcd.[\s\S]*?\*\/)/g;
for(@comments){
print DES_FH "$_\n";
}
# always close the filehandles
close(SRC_FH);
close(DES_FH);
print "File content copied successfully!\n";
uj5u.com熱心網友回復:
我的第二個 if 陳述句中缺少什么?
好吧,沒什么——只是在多行 C 注釋中,它的兩行都沒有/*and */。因此,當逐行讀取檔案時,正則運算式無法匹配多行注釋。
要捕獲此類評論:
將整個檔案讀入一個字串(“slurp”它),然后
/s在正則運算式上添加修飾符,以便也.匹配換行符。還可以使用/g修飾符來捕獲字串中的所有此類模式。單程my $content = do { local $/; # undef record separator so the whole file is read at once open my $src_fh, '<', $src_file or die $!; # have to re-open <$src_fh>; # reads it all }; # lexical filehandle gets closed as we leave scope # NOTE -- there may be difficulties in capturing comments in a C source file my @comments = $content =~ /.../sg; # your regex或者使用庫來 slurp 檔案,比如
use Path::Tiny; my $content = path($src_file)->slurp;
或者,
當你看到時設定一個標志
/*,獲取/列印所有行,直到你點擊關閉*/,然后取消設定標志。這是它的基本版本my $inside_comment = 0; while (<$src_fh>) { if (m{(/\*.*)}) { #/ fix syntax hilite $inside_comment = 1; # opening line for the comment say $des_fh $1; } elsif (m{(.*\*/)}) { # closing line for the comment say $des_fh $1; $inside_comment = 0; } elsif ($inside_comment) { say $des_fh $_} }我測驗了所有這些,但請檢查并改進。一方面,這與前導空格很有趣。
注意:一般來說,從 C 程式中獲取所有注釋可能相當棘手。
這是 slurping 的單行版本
my $file_content = do { local (@ARGV, $/) = $file_name; <> }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464656.html
