finding words that have any combination of certain letters

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
Glen_S
Posts: 8
Joined: Tue May 15, 2007 4:47 pm

finding words that have any combination of certain letters

Post by Glen_S »

I hope this is the right board for this question..

I have a word list in a text file, each word is on its own line. I need to find all words that have any combination of certain letters, for a real simple example: find all words that have the letters a,n,d

Can I enter something in the search box that will show this? If I could show the words that hold all of the letters in the search string would be good, for example the words "and","dna" and "dan" would be found.

But if I could find all words that show any of the letters in the string that would be even better, for example the words "an","a","ad","and", & "dan" would be found.

Is this possible?

thanks in advance!
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

This will match any of the characters in "and":
Find what: [and]

[X] Regular expression
This will match all the words in your list that contain any of the characters in "and":
Find what: ^.*[and].*

[X] Regular expression
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

This will find every instance of those three letters in any combinations.
Search for: [a|n|d]

You can also turn Case Match On/Off.
You could also Bookmark Lines vs. Finding them
Then you could Toggle Bookmarks, Delete Bookmarked lines, and be left with only those words that contain those letters.

-----------------------------------------
[X] Regular expression
Replace All
-----------------------------------------
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
-----------------------------------------
Hope this was helpful.............good luck,
Bob
Glen_S
Posts: 8
Joined: Tue May 15, 2007 4:47 pm

Post by Glen_S »

thanks, that helps. One thing I forgot to add in the OP was can I search for words that has ONLY any of the letters I specify, in any order.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Bob's [a|n|d] should be [and]. An expression [...] matches any of the characters in it, including Bob's |. Inside a [...] construct, | is an ordinary character, not the alternation operator.

This will match all the words in your list that contain only characters in "and":
Find what: ^[and]+$

[X] Regular expression
Glen_S
Posts: 8
Joined: Tue May 15, 2007 4:47 pm

Post by Glen_S »

thanks!, thats what I was looking for!



This is a great little editor! I just have to get more familiar with unix find syntax / terms.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

This has been posted here in various forms a number of times.

There are many regular expression tutorials on the web, and you will find recommendations for some of them if you search this forum.

A standard reference for regular expressions is

Friedl, Jeffrey E F
Mastering Regular Expressions, 3rd ed
O'Reilly, 2006
ISBN: 0-596-52812-4
http://regex.info/

But be aware that the regular expression recogniser used by TextPad is rather weak by the standards of recent tools, so you may get frustrated if you discover a handy trick that doesn't work in TextPad. The recogniser that WildEdit (http://www.textpad.com/products/wildedit/) uses (Boost) is far more powerful.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Thanks for correcting my solution ben_josephs.

Too many languages, too many syantaxes. What was I thinking?

I knew when I sent it that you would improve on it. And I submitted just after your more correct answer. Had I seen yours, I would not have submitted mine.
Hope this was helpful.............good luck,
Bob
Glen_S
Posts: 8
Joined: Tue May 15, 2007 4:47 pm

Post by Glen_S »

thanks again all for the help. I've gone and read the tutorials at regex info page, but I am still trying to figure out how to insert a wild card in my group of letters.

For example I am trying to find all words that start with g that have any of the letters fglop in them, as well as any one of any other letter. I thought that ^g[fglop.] would work as the . would represent any character, but putting the . in makes no difference, and putting it in another set of [] does not work either.

In case anyone is wondering what this is for, I am playing an online game of scrabble with someone and I suspect they are using something like this to find obscure words and they are kicking my *ss here, so I've downloaded the word list and am just trying to level the field a bit. The example I just mentioned is because I have a blank tile.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Are you looking for a regex that matches the words you can make from a given set of Scrabble tiles? That is, are you looking for a regex that matches words consisting of letters only from the given set (which may contain repetitions), each letter to be used at most once? (This is different from the problem as you described it.)

If this is what you want, it's not easy. The problem is constructing the regex. For example, to match words you can make from the leters a r t you can use the regex
a|r|t|ar|at|rt|ra|ta|tr|art|atr|rta|rat|tar|tra
or
a|r|t|art?|atr?|rta?|rat?|tar?|tra?
or
a(rt?|tr?)?|r(ta?|at?)?|t(ar?|ra?)?

The lengths of these regexes become very large very quickly:

From 1 tile of distinct letters you can make 1 "word".
From 2 tile of distinct letters you can make 4 "words".
From 3 tile of distinct letters you can make 15 "words".
From 4 tile of distinct letters you can make 64 "words".
From 5 tile of distinct letters you can make 325 "words".
From 6 tile of distinct letters you can make 1956 "words".
From 7 tile of distinct letters you can make 13699 "words".

A script is what you need here. TexPad doesn't support scripts.
Glen_S
Posts: 8
Joined: Tue May 15, 2007 4:47 pm

Post by Glen_S »

thanks - the ability to find all possible words from a given set of tiles is a "nice to have", I see what you mean by a lot of work to do this.

My main aim was to be able to replicate a blank tile, for example I have the tiles a e i n o s and one blank. I have a letter j as a possible starting letter so I just enter ^j[aeinos]+$ and it finds me all words that start with a j and contain any combo of the letters I have, but I can't figure out how to use the blank here. I though putting a . in the [] with the letters would do it, but it doesn't. I have used the . where I have 2 letters 3 spaces apart for the possible end of a word ie. ^[aeinos]+n.e$ and it worked fine, but how do I get the . to work within the []?
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

I wasn't offering you the ability to find all possible words from a given set of tiles. I was offering you a regex that matches any of those possible words in a given collection of words.

The character class expression [art] matches a, r or t. If a dot in a character class expression matched any character (it doesn't) then [rt.] would match r, t or any character; that is, it would match any character, and would be equivalent to a plain dot. So an "any character" dot would be useless inside a character class expression. Therefore, in character class expression, dot represents itself (so [.] is another way of quoting a dot, equivalent to \.).

To construct a regex that matches words that you can make out of the tiles r, t and blank, you can plug the "any character" dot into the regexes I described above. For example:

^(.|r|t|.r|.t|rt|r.|t.|tr|.rt|.tr|rt.|r.t|t.r|tr.)$
or the equivalent
^(.|.r|.t|r.|t.|.rt|.tr|rt.|r.t|t.r|tr.)$

Of course, you can use the much simpler
^[rt]*.[rt]*$
but that matches words that contain any number of occurrences of the letters on your tiles, such as tart.

Edit: Added equivalent regex.
Post Reply