perl regex 查找任何 5 的倍数 [英] perl regex to find any number that is a multiple of 5
问题描述
Perl 正则表达式查找任何 5 的倍数.
Perl regex to find any numbers that is multiple of 5.
我尝试使用 =~/[5]+/
但它只找到包含 5 的数字,而不是 5 的倍数.
I tried using =~ /[5]+/
but it is finding only numbers which contains 5, but not multiple of 5.
还要找出长度为 5 的倍数的字符串.
And also to find string whose length is multiple of 5.
推荐答案
我会回答你的第二个问题:还要找出长度是 5 的倍数的字符串.
I will answer your second question: And also to find string whose length is multiple of 5.
这比数字部分更适合正则表达式(已回答),只需将 5 个字符分组并匹配它们的倍数
That is more suitable for regex than the number part (that has been answered), just group 5 characters and match multiples of them
^(?:.{5})*$
在 Regexr 上查看 此处
See it here on Regexr
^
和 $
匹配字符串的开头和结尾.
^
and $
matches the start and the end of the string.
.{5}
匹配 5 个字符(不使用 s
修饰符时的换行符除外)
.{5}
matches 5 characters (except newlines when you don't use the s
modifier)
(?:.{5})*
重复组的内部 0 次或更多次 ==> 这也将匹配空字符串!如果您不想要这个并且仅从至少 5 的字符串长度开始,请使用 +
量词表示 1 或更多:^(?:.{5})+$代码>
(?:.{5})*
repeats the inner part of the group 0 or more times ==> this will also match on the empty string! If you don't want this and start only from a string length of at least 5 use the +
quantifier means 1 or more: ^(?:.{5})+$
这篇关于perl regex 查找任何 5 的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!