in reply to Regex question

Hey guys, thank you so far. I see a lot of complex solutions and was hoping for a more simple and elegant regex. The 'stuff' I stril is all but numbers, X and /. So, I always have a clean string like '240x240x2/3600'. Later I convert this string to '240*240*2/3600'. A regex to check this string, '240*240*2/3600' would be preferable. I think i am there. I am testing this: =~ m/^\d{1,4}\*\d{1,4}\*\d{1,4}\/\d{1,4}$/) { Thank you all in advance. Ton

Replies are listed 'Best First'.
Re^2: Regex question
by BillKSmith (Monsignor) on Sep 17, 2023 at 21:04 UTC
    If you truly believed that you always had a 'clean string', you would not need a regex test at all. Your latest regex would match any valid string and much more (in fact, nearly every "invalid_string" in my UPDATE). If you post a list of valid string which demonstrate the variation that is allowed and a list of invalid strings which demonstrate the types of errors you want to detect, we probably can make a simpler regex which does the job. You can modify my UPDATE to test a possible regex against all those strings.

    Please note that every regex suggestion you have received is only one or two lines long. The best solution probably will be a better fit to your problem, but not much shorter.

    Bill
Re^2: Regex question
by talexb (Chancellor) on Sep 17, 2023 at 16:23 UTC

    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.