Regex to find odd letter in string?

2 views
Skip to first unread message

Mike

unread,
Sep 18, 2009, 1:33:32 PM9/18/09
to Regex
I've written an anagram program with PHP and have an "add a letter"
feature. So, for example, someone can put in the word "ache", the
program will show them the anagram "each", but will also show them
"add-a-letter" anagrams like "peach" and "chase". I'd like to
highlight the added letter to make it easier to see. something like:

<b>p</b>each

I'm new to RegEx, but really can't figure out how to identify the
position of the odd letter. This becomes more complicated when there
are duplicate letters in the word, or the added letter is already part
of the original word. For example if I enter the word, "go", the add-
a-letter anagrams would include "goo". In this case, I'd have to
highlight one of the "o"s.

Can someone please show me how to come up with a regex which will
identify the position of a letter within a string that's not included
in the first string?

Much thanks!

Mike

J. Allen R. Day

unread,
Sep 18, 2009, 2:22:48 PM9/18/09
to re...@googlegroups.com
On Fri, Sep 18, 2009 at 12:33 PM, Mike <mi...@pearl-group.com> wrote:
>
> I've written an anagram program with PHP and have an "add a letter"
> feature.  So, for example, someone can put in the word "ache", the
> program will show them the anagram "each", but will also show them
> "add-a-letter" anagrams like "peach" and "chase".  I'd like to
> highlight the added letter to make it easier to see.  something like:

If you've created the program, what's stopping you from adding the
highlighting markup at the time of adding the letter? It would be much
easier than having to compare the before and after strings. You won't
be able to simply use regex to determine what one character is not the
same. If you really must determine what has changed after, though,
you could compare the strings like this:

$before = 'each';
$after = 'peach';
$length = strlen($after);
for($n=0;$n<=$length;$n++) {
$char_before = substr($before,$n,1);
$char_after = substr($after,$n,1);
if ( $char_before != $char_after ) {
$after = substr_replace($after,"<strong>$char_after</strong>",$n,1);
break;
}
}

--
Remember, no matter where you go, there you are. -Buckaroo Banzai

aandmstudios

unread,
Sep 18, 2009, 3:26:27 PM9/18/09
to Regex
That seems like a great approach, but it won't work in this case. I
have a database full of words, the db also has a length field and
count for each letter in the english alphabet. So, in the case of the
word "dog", I'd use the following sql query:

$sql = "SELECT word FROM wordlist WHERE len = 4 AND d_count >= 1 AND
o_count >= 1 AND g_count >= 1";

that leaves me with a list of four letter words with at least one D, O
and G in them. I won't have a clue what or where the extra letter is.

the "Peach" example is simple because the letters are in the same
order and right next to one another. However, searching on the word
"each" we'd also get the words "cheat", and "cache" because they both
include the letters A C E and H and are five letters long.

Any other thoughts?
Reply all
Reply to author
Forward
0 new messages