如何使Perl正则表达式更具可读性?

时间:2015-11-03 23:30:47

标签: regex perl

我有一个填充了数据库字段名的数组,我运行grep命令来删除不需要的字段。在代码中看起来很丑,但它非常有用,因为我们可以很容易地看到数据库中的哪些字段没有传递给另一个程序。 grep命令不喜欢我的回车并且在CR之后立即忽略该字段。有很多丑陋的方法我可以想到解决这个问题以及我在google上看到的许多复杂的方法,但是没有办法忽略greps“//”中的CR?我真的很感谢你们看看这个。

@fieldNames = grep ! /dbid|history|RecordID|CCObjects|MergeSWCRs|AssociatedIntegrationSet|Level1TestResults|
                     Level2TestResults|Level3TestResults|Level4TestResults|Reviews|WithdrawCR|
                     AssociatedWithdrawnCR|Attachments|AssociatedPRs|OriginatingSolution|AssociatedSWRsFull|
                     AssociatedSWRsDelta|ClonedFrom|ClonedTo|AssociatedComment|ExternalLinks|ratl_mastership/, @fieldNames;

2 个答案:

答案 0 :(得分:4)

不要使用正则表达式:

my %ignored = (
    dbid      => 1,
    history   => 1,
    RecordID  => 1,
    CCObjects => 1,
    # ...
);

# or define the hash this way, if you prefer
# my %ignored = map { $_ => 1 } qw(dbid history RecordID CCObjects ...);

@fieldNames = grep { !$ignored{$_} } @fieldNames;

答案 1 :(得分:2)

使用/x modifier使正则表达式更具可读性并忽略空格。

即:

@fieldNames = grep !
    /dbid|history|RecordID|CCObjects|MergeSWCRs|AssociatedIntegrationSet|Level1TestResults|
    Level2TestResults|Level3TestResults|Level4TestResults|Reviews|WithdrawCR|
    AssociatedWithdrawnCR|Attachments|AssociatedPRs|OriginatingSolution|AssociatedSWRsFull|
    AssociatedSWRsDelta|ClonedFrom|ClonedTo|AssociatedComment|ExternalLinks|ratl_mastership/x,
    @fieldNames;

或者可能略微优化:

my @fieldNames =
    grep ! /
        dbid|
        history|
        RecordID|
        CCObjects|
        MergeSWCRs|
        Level[1-4]TestResults|
        Reviews|
        WithdrawCR|
        Associated(?:WithdrawnCR|PRs|SWRsFull|SWRsDelta|Comment|IntegrationSet)|
        Attachments|
        OriginatingSolution|
        Cloned(?:From|To)|
        ExternalLinks|
        ratl_mastership
    /x,
    @fieldNames;