Match multiple number

by IB2017 (Pilgrim)
 on Aug 17, 2018 at 18:37 UTC Need Help??

IB2017 has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks

How can I write the following regex so that it matches ad infinitum all numbers that are x=x+5? In my regex I stopped at 25.

```if (\$Number=~/0|5|10|15|20|25/){
...
}

Replies are listed 'Best First'.
Re: Match multiple number
by tobyink (Canon) on Aug 17, 2018 at 18:45 UTC

Use modulo arithmetic.

```if (\$Number % 5 == 0){
...;
}

Alternatively, as all multiples of five end in a 5 or a 0, you can do something like this:

```if (\$Number =~ /^-?[0-9]*[05]\$/){
...;
}

In both these cases, I'm allowing negative values, like -5 and -10. If you prefer to only allow non-negative values:

```if (\$Number % 5 == 0 and \$Number >= 0){
...;
}
```if (\$Number =~ /^[0-9]*[05]\$/){
...;
}

Thank you very much. I like these solutions.

Re: Match multiple number
by davido (Cardinal) on Aug 17, 2018 at 22:21 UTC

Totally useless but a little bit of fun with a silly mathematical property:

```#!/usr/bin/env perl
+

+
use strict;
+
use warnings;
+
use List::Util qw(sum);
+

+
# For each \$n, convert \$n to hex digits, add the digits together. If t
+he
# result has more than one hex digit, repeat this process adding the
+
# digits together. When there is only one hex digit remaining, if the
+
# digit is a '5', an 'a', or an 'f', the original number was divisible
+
# by 5.
+

+
for my \$n (1..100) {
+
my \$h = sprintf '%x', \$n;
+
while(length(\$h) > 1) {
+
\$h = sprintf '%x', sum(map {hex \$_} split //, \$h);
}
+
print "\$n is a multiple of 5\n" if \$h =~ m/^[5af]\$/;
+
}

Output:

```5 is a multiple of 5
10 is a multiple of 5
15 is a multiple of 5
20 is a multiple of 5
25 is a multiple of 5
30 is a multiple of 5
35 is a multiple of 5
40 is a multiple of 5
45 is a multiple of 5
50 is a multiple of 5
55 is a multiple of 5
60 is a multiple of 5
65 is a multiple of 5
70 is a multiple of 5
75 is a multiple of 5
80 is a multiple of 5
85 is a multiple of 5
90 is a multiple of 5
95 is a multiple of 5
100 is a multiple of 5

Dave

Interesting. I guess this works for the same reason it does with 3 in decimals.

Re: Match multiple number
by jwkrahn (Monsignor) on Aug 17, 2018 at 18:47 UTC
```if ( \$Number =~ /\d*[05]/ ) {
```my \$Number = '!56';
if ( \$Number =~ /\d*[05]/ ) {
say "\$Number is divisible by 5.";
warn 'Really?';
}

A dollar sign missing somewhere at least.

```(\$q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+\$1,++print+eval\$q,q,a,
Re: Match multiple number
by dbuckhal (Hermit) on Aug 19, 2018 at 00:06 UTC

Another similar solution:

```\$ perl -e '
for (0..1000) {
my \$n = int(rand(10000));
print \$n, "\n" if \$n =~ /\d{0,}[05]\z/
}
'
__sample_output__
7495
8120
8340
9540
7370
4455
310
7540
35
7085
7235
5355
9190
6790
1360
820
1900
1945
1630
9895

This matches 1.05.

yeah, but it satisfies the OP's implied set of integer values given by x=x+5 | {0, 5, 10, 15, 20, 25, ...}

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1220530]
Approved by tobyink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-05-16 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you prefer to work remotely?

Results (63 votes). Check out past polls.

Notices?