#!/usr/bin/perl use strict; use warnings; $\ = "\n"; $, = "\t"; while () { chomp; my ( $pfx, $num ); if ( /^\d+$/ ) { $pfx = ''; ( $num = $_ ) =~ s/^0+//; } elsif ( /^\S+$/ ) { ( $pfx, $num ) = ( /(\D+)0+(\d+)/ ); } else { my $last_space = rindex( $_, ' ' ) +1; ( $pfx = substr( $_, 0, $last_space )) =~ s/\s+$//; ( $num = substr( $_, $last_space )) =~ s/^([^0]*)0+//; $pfx .= " $1" if ( length( $1 )); } print $_, $pfx, $num; } __DATA__ XYZ 123 00654321 XYZ 12 ST 00123456 XYZ 123 ST 00654321 XYZ U 123 00123456 XYZ U 12 00654321 XYZ V 1 00123456 XYZ 12300654321 XYZ 00123456 XYZ 0654321 ABC-M-0123456 ABCD-00654321 00000123456 #### if ( $num !~ /^\d{1,7}$/ or $pfx !~ /^(?: ABC(?:D|-M)- | XYZ(?:\s[UV])? (?:\s\d{1,3} (?:\sST)? )? )$/x ) { warn "Bad input at line $.\n"; next; }