开发者

Regular Expression for @ Array in Perl

Actually i m using one perl script to prepare Readme txt for my builds. in that script i used

foreach $line (<LOG>) 
{   
    if(length(trim($line))>0)
    {
    $line=trim($line);
    $line=~ s/[\r]//gs;
    if(rindex($line,'#')!=-1)
    {
    $icut=substr($line,0,index($line,']'));
    $icut2=substr($icut,index($icut,'#')+1,length($icut));
    }
    push(@issue,$icut2);

it's fetching correct issue no but when the situation comes like

[I#1303350], [I#1270918],[I#1312521] Updated Physical Confirmation Template based on CO

then it's fetching only one issue no not all issue i that same line. so i modified my code like

foreach $revno(<REV>)
{
    if(length(trim($revno))>0)
    {
        $revno=trim($revno);
        $revno=~ s/[\r]//gs;
        if(rindex($revno,'#')!=-1)
        {
            $revcut=substr($revno,0,rindex($revno,']'));
            print "$revcut\n";
            $revcut1=substr($revcut,index($revcut,'#')+1,length($revcut));
        }
    }
    push(@issue,$revcut1);

now it's fetch it all revision no but output is like 1312588,1303350], [I#1270918开发者_C百科],[I#1312521 but i want to remove the # [ ] I only but not , so pls tell me how can i parse this through regex.


This can be done without regular expressions: Transliterate: tr///

use warnings;
use strict;

my $s = '1312588,1303350], [I#1270918],[I#1312521';
$s =~ tr/ ][#I//d;
print "$s\n";

__END__

1312588,1303350,1270918,1312521


You can do it like this:

echo "[I#1303350], [I#1270918],[I#1312521]" | perl -lnwe "print for m/#(\d+)/g"


This works for me:

 my @issues = $line_of_data =~ m/#(\d+)/g;

And if you want commas, it's far easier to do this:

my $with_commas = join( ', ', @issues );

And you still have the individual issues numbers as "atoms" of data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜