Find the line with multiplied length

I need to find every word whose length is a multiple of 7 and less than 35. I can use some template, for example

/\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 will be a better solution, for example

[a-zA-Z0-9]{7|14|21|28} 

or even how

[a-zA-Z0-9]{7*k}
+4
source share
4 answers

Something like this should do the trick

/\b(?:[a-zA-Z0-9]{7}){1,5}\b/

which corresponds to strings of length 7,14,21,28,35

Demo: https://regex101.com/r/eO4oG3/2

EDIT: Another possibility is to use backlinks
http://www.regular-expressions.info/backref.html

+8
source

This should work:

\b([[:alnum:]]{7}){1,5}\b

It corresponds to each symbol of the class [A-Za-z0-9], to each subword length 7and its product p 5.

eg.

12345672234567
abcdefghijklmn

but not 12345678

\w [[: alnum]], POSIX .

+2

Assuming you break words on a character other than a word (use String#splitif breaking into spaces):

text.scan(/\w+/).select { |word| [7, 14, 21, 28].include?(word.size) }
0
source

This is the best way, and closest you can use regex.

(?i)\b(?=[a-z\d]{1,34}\b)(?:[a-z\d]{7})+\b

Below: '34'- maximum variable length, '7'- factor factor variable.
They are independent of each other.
You can build this at runtime with any length and variable values.

 (?i)                         # No case
 \b                           # Word boundary
 (?= [a-z\d]{1,34} \b )       # Max length = 34
 (?: [a-z\d]{7} )+            # Multiple factor = 7
 \b                           # Word boundary

Input:

length=7  aaaaaaa   
length=14 bbbbbbbbbbbbbb
length=21 ccccccccccccccccccccc
length=28 dddddddddddddddddddddddddddd
length=35 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

Output:

 **  Grp 0 -  ( pos 10 , len 7 ) 
aaaaaaa  

-------------

 **  Grp 0 -  ( pos 32 , len 14 ) 
bbbbbbbbbbbbbb  

-------------

 **  Grp 0 -  ( pos 58 , len 21 ) 
ccccccccccccccccccccc  

-------------

 **  Grp 0 -  ( pos 91 , len 28 ) 
dddddddddddddddddddddddddddd  
0
source

All Articles