Scrabble Regex Help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Scrabble Regex Help
# 1  
Old 03-30-2013
Scrabble Regex Help

Hi!
I use an and grep to play Scrabble and other word games because it helps me practice regexes, or, as my friends would say, because I'm a cheater.
Anyway, I'm trying to work out how to write a regex that might do what I want, so let's see if I can explain it properly. Let's say we want to play down. We have a letter, F, followed by three spaces, then a D, which is at the end of the board. The space below the F is a DW, so we want to play it, if possible. We have plenty of room above the F, but we can only play two spaces below F if we can't use the D, three if we can use the D. I have attached a screen-shot of the board if you're having a hard time visualizing it. I have already played "famed."
So, here's the regex I tried:
Code:
an -w -m3 -d twl 'qreraumfd' |grep -E '.*f.{1,3}d?$'

We anagram my rack, plus the F and D, and pipe it to grep. Here we're saying we can have one to three characters after the F, and the D can be there or not. While it returns many usable results, it also returns results which ignore the D. For instance:
Code:
fard
farm
fear

which can't be used because you'd have "fardd" or "farmd", and so on.
Perhaps you are thinking, "Why not just ignore the four-letter words it returns?" And certainly I could pipe it to grep one more time to eliminate them, but I want to know if I can do it all in one grep ERE.
I've tried it a few different ways, but can't quite work it out:
Code:
an -w -m3 -d twl 'qreraumfd' |grep -E '.*f.{1,2}.?d?$'

returns identical results.
What do you think? Is it possible?
Scrabble Regex Help-scrabblejpg
# 2  
Old 03-30-2013
As you said, you can have "f" and two letters following, or, "f" and three letters and "d".
Why not use two ored regexs: grep -E ".*f.{1,2}|.*f.{3}d"?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-30-2013
Quote:
Originally Posted by RudiC
As you said, you can have "f" and two letters following, or, "f" and three letters and "d".
Why not use two ored regexs: grep -E ".*f.{1,2}|.*f.{3}d"?
Aha! Yes, it works if you "cap off" the ends with a $

Code:
grep -E ".*f.{1,2}$|.*f.{3}d$"

I forgot you could do that. I was trying to figure out a way to do it with a lookaround somehow. Thanks!
# 4  
Old 03-30-2013
Anchoring them at line end is a good idea - I had no chance to test it so missed that...
Try also the "abbreviated" pattern ".*f(.{1,2}|.{3}d)$"
This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. UNIX for Dummies Questions & Answers

Scrabble (SED)

Hello everyone, I've been struggling with an assignement for school for quite some time now and I decided to make a thread here. The goal of the script is to return all the words that can be created with a certain combination of letters that can be given as an argument for the shellscript. It's... (1 Reply)
Discussion started by: iFaceDive
1 Replies

4. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies
Login or Register to Ask a Question