Here is my take (my takes actually):
#!/bin/perl -w use strict; my $numbers="1 1.0 3.547 92.34 12 .25 23.00 23.01 343.2234"; # I guess that's what you were looking for while($numbers=~/(?<![\.\d]) # first no digits or . (\d+) # digits (?!\d*\.\d) # then no digits, . and digits /gx) { print "$1 is an integer\n"; } print '-' x 20, "\n"; # the same thing slightly simpler while($numbers=~/(?:\A|[^\d\.]) # catches the character before a numb +er or the start of the string (\d+) # digits (?!\d*\.\d) # then no digits, . and digits /gx) { print "$1 is an integer\n"; } # much simpler I think while($numbers=~/([\d\.]+)/g) # catch all numbers { if( $1=~ /^(\d+)$/) # keep only integer ones { print "$1 is an integer\n"; } # this $1 from the if regexp } print '-' x 20, "\n"; # now maybe 1.0 is considered an integer while($numbers=~/(\d+(?:\.(\d+))?)/g) { my $nb= $1; # get all + numbers print "$nb is an integer\n" if( !$2 || ($2=~ /^0+$/) ); # keep th +ose with no decimal parts # or with + a deciaml part only made of 0's }
gives:
1 is an integer 12 is an integer -------------------- 1 is an integer 12 is an integer -------------------- 1 is an integer 12 is an integer -------------------- 1 is an integer 1.0 is an integer 12 is an integer 25 is an integer 23.00 is an integer
In reply to Re: How can I match all the integers in a string?
by mirod
in thread How can I match all the integers in a string?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |