in reply to Re: Regex question
in thread Regex question
This looks like it works ..
This assumes that you've stripped out the spaces.tab@music4:~/Pianoforte/Development/Perlmonks 12:19:50 $ cat 11154494. +pl #!/usr/bin/perl use strict; use warnings; my $str = '240*240*2/3600'; use Test::More; { like ( $str, qr{^(\d+[*/])+(\d+)$}, 'Check regex' ); done_testing; } tab@music4:~/Pianoforte/Development/Perlmonks 12:19:55 $ prove !$ prove 11154494.pl 11154494.pl .. ok All tests successful. Files=1, Tests=1, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.06 cusr + 0.00 csys = 0.09 CPU) Result: PASS tab@music4:~/Pianoforte/Development/Perlmonks 12:19:58 $
The regex I've used is just 'number plus operator' repeated at least once, followed by 'number'. So this should work for unsigned integers. You'd need to expand the regex for signed numbers, fractional numbers, numbers with exponents, numbers in a base higher than ten, and so forth.
Update: And if you want to look for a more specific pattern (as in your original post, sorry!)
tab@music4:~/Pianoforte/Development/Perlmonks 12:33:45 $ cat !$ cat 11154494-2.pl #!/usr/bin/perl use strict; use warnings; my $str = '240*240*2/3600'; use Test::More; { like ( $str, qr{^(\d+\*){2}(\d+)/(\d+)$}, 'Check regex' ); done_testing; } tab@music4:~/Pianoforte/Development/Perlmonks 12:33:50 $ prove !$ prove 11154494-2.pl 11154494-2.pl .. ok All tests successful. Files=1, Tests=1, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.05 cusr + 0.00 csys = 0.08 CPU) Result: PASS tab@music4:~/Pianoforte/Development/Perlmonks 12:33:58 $
|
|---|