Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a string of multiplicy length

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}
like image 267
Ed Akhmetshin Avatar asked Dec 25 '22 18:12

Ed Akhmetshin


2 Answers

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

like image 55
bro Avatar answered Jan 09 '23 02:01

bro


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.

like image 35
nerdbeere Avatar answered Jan 09 '23 02:01

nerdbeere