This looks like it works ..

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 $
This assumes that you've stripped out the spaces.

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 $

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.


In reply to Re^2: Regex question by talexb
in thread Regex question by Umdurman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.