ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

I have three types of inputs in my pipe separated csv file. In that thirteenth field is report error field. In that field
Error from hlr : cannot connect to smsc Error from MSc : unsubscribe error No paging
Like this three types of errors are there. In that in need only error. I don't need where it comes from. So I used (split /:/) but its taking spaces in the starting of that error. And the last one does not have any thing to split. How to write regex for this???

Replies are listed 'Best First'.
Re: Regex for splitting
by Athanasius (Cardinal) on Jan 03, 2016 at 07:06 UTC

    Hello ravi45722,

    If I understand you correctly, you may be looking for something like this:

    #! perl use strict; use warnings; my @error_fields = ( ' Error from hlr : cannot connect to smsc ', 'Error from MSc : unsubscribe error', 'No paging ', ); for my $field (@error_fields) { $field =~ / ^ (?: .*? : )? \s* (.*?) \s* $ /x; print "|$1|\n"; }

    Output:

    17:05 >perl 1503_SoPW.pl |cannot connect to smsc| |unsubscribe error| |No paging| 17:05 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Regex for spliting
by Laurent_R (Canon) on Jan 03, 2016 at 09:24 UTC
    Hi, you could split on a colon and get the last field of the resulting array (the item with the -1 index):
    for my $str (@strings) { my @array = split /:/, $str; print "$array[-1] \n"; }
    Update: fixed a typo in the above code (asemi-colon instead of a colon in the split regex). Thanks to CountZero for pointing it out to me.