CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    22

    Regex - finding words with two e's

    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.

    Code:
    /\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.

  2. #2
    Join Date
    Jun 2004
    Posts
    142

    Re: Regex - finding words with two e's

    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;

    Code:
    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
    Code:
    if(strtoupper($checkletters) == strtoupper("e")){
    note that my function is simply echoing stuff... you might want to "return" a value if true or false...
    Last edited by bobo; March 17th, 2010 at 02:34 PM.

  3. #3
    Join Date
    Dec 2009
    Posts
    22

    Re: Regex - finding words with two e's

    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.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Regex - finding words with two e's

    Code:
    /\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.
    Last edited by PeejAvery; March 17th, 2010 at 04:15 PM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured