-1

I've been for a while trying to setup a regex in mysql where it finds words from a dictionary table whose letters are in set.

Giving a set of letters i.e. "freedom" I want to get what combination of words i can obtain from such set of letters:

The result should be something like:

"free", "dom", "fee", "freedom", "red" and so on....

Is it possible using regex?

Thanks in advance.

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
domoindal
  • 1,513
  • 2
  • 17
  • 33

1 Answers1

3

It's definitely NOT possible using regex.

It IS possible, though, using a combination of combinatorics & a really good wordlist.

In a few words : what you're trying to do IS both challenging and difficult, but there could be a way...

What I would do :

  • Find all possible "words" (combinations of letters, up to size N - whatever)
  • Check each and every one of those, if it's in your wordlist (Dictionary)
  • if it IS, then count it up. If not, go on.

Community
  • 1
  • 1
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • Well, lets say i want to find all combinations of words of three letters that contains a subset of these letters "appiuxs". the regex i use is REGEXP '^[appiuxs][appiuxs][appiuxs]', but it doenst work fine. it finds words of any size and i want only three. – domoindal Apr 14 '12 at 01:58
  • @domoindal if all you want is to MATCH combinations of 3 of whatever leters (e.g. a,p,i,u,x,s), all you have to is : `([apiuxs]+){3}`. Have a look at the demo here : http://regexr.com?30kv5 – Dr.Kameleon Apr 14 '12 at 02:07
  • @domoindal The `{3}` part in the end means **exactly** what need : MATCH [apiuxs] EXACTLY three times... ;-) – Dr.Kameleon Apr 14 '12 at 02:07
  • I used this sentence in MySQL: SELECT * from palabras where palabra REGEXP '([ave]+){3}', and it returns words with more than 3 letters and letters that do not belong the set. It the same problem I've been having.... – domoindal Apr 14 '12 at 02:11
  • @domoindal Oh, that's a different thing. I know found out : Whay don't you try something like `SELECT * FROM palabras WHERE palabra REGEXP '([ave]+){3}' AND LENGTH('palabra') = 3` ? – Dr.Kameleon Apr 14 '12 at 02:14
  • checked and now it doesnt return anything. I thing it's not possible in mysql, i think i will have to build in PHP sentences with LIKE. I thought that with Regex i could simplify the query. – domoindal Apr 14 '12 at 02:18