chell chell - 26 days ago 7
Ruby Question

How to select words that are made up of the same letter using regex?

I have a dictionary text file that contains some words that I don't want.

Example:
aa
aaa
aaaa
bb
b
bbb
etc

I want to use a regular expression to select these words and remove them. However,
what I have seems to be getting too long and there must be a more efficient approach.

Here is my code so far:

/^a{1,6}$|^b{1,6}$|^c{1,6}$|^d{1,6}$|^e{1,6}$|^f{1,6}$|^g{1,6}$|^[i]{2,3}$/


It seems that I have to do this for every letter. How could I do this more succinctly?

Answer

You can try with this regex:

\b([a-z])\1{0,}\b

and replace by empty

Ruby code sample:

re = /\b([a-z])\1{0,}\b/m
str = 'aa aaa aaaa bb b bbb abc aa a pqaaa '
result = str.gsub(re,'')
puts result

Run the code here

Comments