Re: show only numbers before, within or after the text



Luca Villa wrote:
I have a long text file like this:

324yellow
34house
black
54532
15m21red56
44dfdsf8sfd23

How can I obtain (Perl - Windows/commandline/singleline) the
following?

1) only the numbers at the beginning before some alpha text, like
this:

324
34
15
44

2) only the numbers within the alpha text, like this:

21
8

3) only the numbers at the end after some alpha text, like this:

56
23

Something like this?

HTH,

Rob


use strict;
use warnings;

my @data = qw/
324yellow
34house
black
54532
15m21red56
44dfdsf8sfd23
/;

foreach (@data) {

print "$_:\n";

while (/(^|\D)(\d+)/g) {

my $bef = not $1;
my $val = $2;
my $aft = not /(?=\D)/gc;

if ($bef) {
print "'$val' found before text\n" unless $aft;
}
elsif ($aft) {
print "'$val' found after text\n";
}
else {
print "'$val' found within text\n";
}
}

print "\n";
}

**OUTPUT**

324yellow:
'324' found before text

34house:
'34' found before text

black:

54532:

15m21red56:
'15' found before text
'21' found within text
'56' found after text

44dfdsf8sfd23:
'44' found before text
'8' found within text
'23' found after text
.



Relevant Pages