Perl regex to find any multiple of 5

Perl regex to find any numbers that are multiples of 5.

I tried using =~ /[5]+/ but it only finds numbers containing 5 but not multiples of 5.

And also find a string whose length is a multiple of 5.

+4
source share
5 answers

I will answer your second question: and also find a string whose length is a multiple of 5.

This is more suitable for regular expression than the number number (which was answered), only 5 characters and match with them

 ^(?:.{5})*$ 

See here at Regexr

^ and $ matches the beginning and end of a line.

.{5} matches 5 characters (except for newlines if you do not use the s modifier)

(?:.{5})* repeats the inside of the group 0 or more times ==> this will also correspond to an empty line! If you do not want this and start only with a line length of at least 5, use the quantifier + means 1 or more: ^(?:.{5})+$

+1
source

Multiples of 5 end with 5 or 0.

Try using /^-?\d*[05]$/ , which means:

  • ^ start of line (saflknfvs34535 will not work).
  • -? Minus sign or not (if you want only positive numbers, do not put this).
  • \d* numbers, any numbers.
  • [05] 0 or 5.
  • $ end of line (324655sefgsfgsfg not working).
+6
source

For integers

 ($_ % 5) == 0 

or

 !($_ % 5) 
+6
source

These numbers end in 0 or 5 , so something like m/^\d*[05]$/

+4
source

Just for fun, here is a more general solution that works for any divisor:

 use 5.010; "150" =~ /^(\d+)(?(?{ $^N % 5 == 0 })|(*FAIL))$/; 
+1
source

Source: https://habr.com/ru/post/1480850/


All Articles