I received a desktop day-to-day calendar with puzzles. One such puzzle was deciphering a quote where the letters were substituted with symbols. I used some RegExs to find longer words, then used the returned words to solve smaller words. In the puzzle, white background symbols were vowels (including 'y') and shaded background symbols were consonants.
I'll use random letters below where bolded means consonant, plain means vowel, and italicized means the letter was given in the directions.
BOQQE
The example above was deciphered as "happy" ('e' was already given in the puzzle) by using the RegEx
egrep -i '^[bcdfghjklmnpqrstvwxz][aiouy][bcdfghjklmnpqrstvwxz]{2}[aiouy]$' words
There were a lot of results, but I feel I could have made the RegEx better by specifying the RegEx logically as
- Char 1 is a consonant
- Char 2 is a vowel, but not 'e' because that was given in the directions.
- Chars 3 & 4 are the same consonant, but are different from char 1.
- Char 5 is a vowel, but different from char 2.
Another example would be
O R e W Y D O n
where I used the grep statement
egrep -i '^([aiouy])[bcdfghjklmnpqrstvwxz]e[bcdfghjklmnpqrstvwxz][aiouy][bcdfghjklmnpqrstvwxz]\1n$' words
to logically define the search as
- Char 1 is a vowel, and is a captured group because the same char appears later in the word.
- Char 2 is a consonant.
- Char 3 is 'e', given.
- Char 4 is a consonant.
- Char 5 is a vowel.
- Char 6 is a consonant.
- Char 7 is the same vowel as Char 1.
- Char 8 is 'n', given.
Fortunately, the grep statement returned one word, "American" (The cipher-text was a movie quote). I would like to have been able to specify in the RegEx that Char 4 is a consonant and not the same as char 2, char 5 is a vowel and not the same as char 1, etc.
Is it possible to ask this kind of pattern matching with RegExs? I'm aware of the (x|y)
syntax to state that a character may be 'x' or 'y', but I don't know of the syntax, if it exists, to specify (!x) & y
Best Answer
You can using perl regular expressions that have negative look-aheads.
Expanded:
The BOQQE example would be
which returns 779 results (444 case sensitive) with my dictionary.