簡體   English   中英

如何使用 perl 從文件中搜索和提取注釋行?

[英]How do i search and extract commented lines from a file using perl?

我是 Perl 編程的新手。 需要您的幫助才能從文件中搜索和提取注釋行。 下面是一個例子

{
    This is line 1
    /*This is line 2
    This is line 3
    This is line 4*/
    This is line 5
}

我只想從上面的文件中搜索和提取注釋行。

您可以使用這樣的正則表達式:

\/\*([\s\S]*?)\*\/

工作演示

代碼

my $str = 'you string';
if ( $str =~ /\/\*([\s\S]*?)\*\// ) {
    print "Comment: $1";
}

正如鮑羅丁在他的評論中指出的那樣,您可以使用帶有s標志(單行)的點而不是[\\s\\S] ,因此您可以將正則表達式更改為:

\/\*(.*?)\*\/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM