【问题标题】:Read the line from the particular index till the end in a file in perl在 perl 中读取特定索引中的行直到文件末尾
【发布时间】:2015-01-08 07:28:34
【问题描述】:

我有以下内容的日志文件:

(8092) "DEFECT_AUDIT_INTTEST_FRI_JAN_02_2015_07_05_09" (3 of 4)
(7992) ---$ FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM
(8007) ---$ FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM
(7994) ---$ FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM

我想读取每个文件并存储遵循以下标准的内容,即 一个。线路有--- 湾。以 --- 开头的内容,后跟特殊字符 $ 和空格。例如。 在这里,我想要大小为 3 并具有以下内容的数组: FirstName1 Surname1 “评论编号 1” 2015 年 1 月 2 日上午 1:53 FirstName2 Surname2 “评论编号 2” 2014 年 12 月 19 日上午 6:20 FirstName3 Surname3 “评论编号 3” 2014 年 12 月 19 日上午 6:46 我当前的代码是:

if($_ =~/---$/){
    my ($CsDescription) = /"---$ "/;
    push @CSArray , $CsDescription;
}

【问题讨论】:

  • 如果你匹配$而没有\$这意味着你正在查看一个以---符号结尾的字符串

标签: regex perl


【解决方案1】:

...以下元字符具有[特殊]含义:

\        Quote the next metacharacter
^        Match the beginning of the line
.        Match any character (except newline)
$        Match the end of the string (or before newline at the end
         of the string)
|        Alternation
()       Grouping
[]       Bracketed Character class 

http://perldoc.perl.org/perlre.html

括号字符类中的特殊字符
大多数字符 是正则表达式中的元字符(即字符 带有特殊含义(如 .、* 或 () 的)失去其特殊意义 含义并且可以在字符类中使用而无需 逃离他们。例如,[()] 匹配左括号, 或右括号,以及字符类中的括号 不要分组或捕获。

在字符类中可能带有特殊含义的字符 是:\、^、-、[ 和 ],并在下面讨论。他们可以逃脱 带反斜杠,虽然有时不需要,在这种情况下 反斜杠可以省略。

http://perldoc.perl.org/perlrecharclass.html#Bracketed-Character-Classes

use strict;
use warnings;
use 5.016;

my @lines;

my $regex = qr{
    .*?     #Match any character, 0 or more times, non-greedy, followed by...
    -{3}    #a dash, 3 times, followed by...
    \$      #a dollar sign, followed by...
    [ ]     #a space, followed by...
    (.*)    #any character, 0 or more times, captured in $1
}xms;


for my $line (<DATA>) {
    if ($line =~ $regex) {
        push @lines, $1;
    }
}

print for @lines;

__DATA__
(8092) "DEFECT_AUDIT_INTTEST_FRI_JAN_02_2015_07_05_09" (3 of 4)
(7992) ---$ FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM
(8007) ---$ FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM
(7994) ---$ FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM

输出:

FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM
FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM
FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM

正则表达式中的大多数元字符(即带有特殊含义的字符,如 .、* 或 () 失去了它们的特殊含义,并且可以在字符类中使用而无需对其进行转义。对于例如,[()] 匹配左括号或右括号,并且字符类中的括号不会分组或捕获。

在字符类中可能带有特殊含义的字符有:\、^、-、[ 和],这些将在下面讨论。它们可以用反斜杠转义,尽管有时不需要,在这种情况下可以省略反斜杠。

http://perldoc.perl.org/perlrecharclass.html#Bracketed-Character-Classes

关于 $??!

【讨论】:

    【解决方案2】:

    您可以直接使用$,因为您使用的是$ perl 作为行尾 并删除内容直到找到$ 符号

    if(/---\$/){
        my $CsDescription=$_;                                                    
        $CsDescription =~s/.*\$// ;
        push @CSArray , $CsDescription;
    }
    
    
    print "@CSArray" ;
    

    输出:

    FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM
    FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM
    FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM
    

    【讨论】:

    • 非常感谢您的详细解释
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2011-11-09
    • 2012-08-26
    • 2015-01-23
    相关资源
    最近更新 更多