I have to find every word whose length is a multiple of 7 and less than 35. I can use some pattern like
/\b([a-zA-Z0-9]{7}|[a-zA-Z0-9]{14}|[a-zA-Z0-9]{21}|[a-zA-Z0-9]{28})\b/
but I hope that there's some better solution like
[a-zA-Z0-9]{7|14|21|28}
or even like
[a-zA-Z0-9]{7*k}
Something like this should do the trick
/\b(?:[a-zA-Z0-9]{7}){1,5}\b/
which matches strings of length 7,14,21,28,35
Demo: https://regex101.com/r/eO4oG3/2
EDIT: Another possibility would be using backreferences
http://www.regular-expressions.info/backref.html
This one should work:
\b([[:alnum:]]{7}){1,5}\b
It matches every character of the class [A-Za-z0-9]
, every sub-word of length 7
and its product with 5
.
e.g.
12345672234567
abcdefghijklmn
but not 12345678
EDIT Replaced the \w with [[:alnum]] as this POSIX class contains only letters and digits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With