Click to See Complete Forum and Search --> : Regex - finding words with two e's


jc987
March 17th, 2010, 12:53 PM
Hello again, another question from me!

Alright I am working on an assignment. And I haven't been able to figure out what the regex syntax would be to search for words, within a string, with atleast 2 e's. This is what I have right now.

/\w+e.?+e.?+/

but this only finds words like 'here'. I need it to return the word no matter where the e's are in the word, as long as there are atleast 2 in the word.

Thanks in advance for any help.

bobo
March 17th, 2010, 01:45 PM
there's probably more efficient ways of doing this but here is the logic I would use..

I did not test this, but it should work;


function getWordsWithDblEs($myWord){
$myLetterArray = str_split($myWord);
$i=0;
foreach($myLetterArray as $checkletters){
if($checkletters == "e"){
$i++;
}
}
if($i >= 2){
echo "the letter \"e\" is found ".$i." times in the word ".$myWord;
}else{
echo "this word have less than two e";
}
}


you might want to handle cap sensitivity... and have line 5 say

if(strtoupper($checkletters) == strtoupper("e")){


note that my function is simply echoing stuff... you might want to "return" a value if true or false...

jc987
March 17th, 2010, 03:47 PM
My teacher wants us to use regular expressions.

What the script does is read a file and store the lines in an array. I am finding only the word that are e{any letter}e (here, there, were, etc...) and then it out puts the number of words in each string that have at least 2 e's. I am not counting the number of e's in each word.

PeejAvery
March 17th, 2010, 04:12 PM
/\w*ee\w*|\w*e[^>]*e\w*/i

Two conditions separated by a |

Any word containing "ee" back to back.
Two "e" characters separated by any other character...all within the word boundary.