Hi all,

I dont why my code is not detecting the matching words.
Firstly Im reading the words from a text file into an array.

Plz help me to make this correct.
This is my code.

import java.util.*;
import java.io.*;
public class TestRegex{
public static void  main(String[] arg)
{

String s="ace";
int i=0;
/*if (s.matches(".*c.*e.*"))
System.out.println("ok");

for(char alphabet = 'A'; alphabet <= 'Z';alphabet++){
 System.out.println(alphabet);
 }
*/
   String[] arrayWords=new String[120];
 /*  arrayWords[0]="apcple";
   arrayWords[1]="appa";
   arrayWords[2]="esjdchc";
   arrayWords[3]="csksjce";
   arrayWords[4]="ce";
   arrayWords[5]="jazz";
   arrayWords[6]="classic";
   arrayWords[7]="sjdheiudc";
   arrayWords[8]="no";
   arrayWords[9]="yes";*/
 try
    {
        FileInputStream fileStm = new FileInputStream("words.txt");
        DataInputStream ins = new DataInputStream(fileStm);// Get the object of DataInputStream
        BufferedReader br = new BufferedReader(new InputStreamReader(ins));
        String line;
        String delims = " ";//Delimeters

        while ((line = br.readLine()) != null)//Read the file Line By Line
        {
                    StringTokenizer st = new StringTokenizer(line, delims,false);//split the line into tokens according to provided delimas
            //numTokens = st.countTokens();//count the number of tokens in the line

            while(st.hasMoreTokens())//if there is a token go into the loop
            {
                            String myToken=st.nextToken();
                            arrayWords[i]=myToken;
                            i++;
            }

        }

                }catch (IOException io)
                {
                    System.out.println("IO exception");
                }

                System.out.println("size : "+i);
                String pattern=".*c.*e.*";
            //  System.out.println(arrayWords[10]);
        String str=null;

        for (int c=0;c<10;c++)
        {
   //  System.out.println(arrayWords[c]);
          str=(String)arrayWords[c];
            if (str.matches(pattern))
            {

            System.out.println(arrayWords[c]);

            }
         }

}

}

Did your regex work with the array of test data (now commented out)?
Have you printed the actual data in the String array after reading your file to see if the file input is doing what you expect?

Yes.The array elements are printed.When i read the words from the file into the array, the regex is not detecting. These are the words in the files:

AAHED AALII AARGH ABACA ABACI ABACK ABAFT ABAKA ABAMP ABASE ABASH ABATE ABBAS ABBES ABBEY ABBOT ABEAM ABELE ABETS ABHOR ABIDE ABLER ABLES ABMHO ABODE ABOHM ABOIL ABOMA ABOON ABORT ABOUT ABOVE ABRIS ABUSE ABUTS ABUZZ ABYES ABYSM ABYSS ACARI ACERB ACETA ACHED ACHES ACHOO ACIDS ACIDY ACING ACINI ACKEE ACMES ACMIC ACNED ACNES ACOCK ACOLD ACORN ACRED ACRES ACRID ACTED ACTIN ACTOR ACUTE ACYLS ADAGE ADAPT ADDAX ADDED ADDER ADDLE ADEEM ADEPT ADIEU ADIOS ADITS ADMAN ADMEN ADMIT ADMIX ADOBE ADOBO ADOPT ADORE ADORN ADOWN ADOZE ADULT ADUNC ADUST ADYTA ADZES AECIA AEDES AEGIS AEONS AERIE AFARS AFFIX AFIRE AFOOT AFORE AFOUL AFRIT AFTER AGAIN AGAMA AGAPE AGARS AGATE AGAVE AGAZE AGENE AGENT AGERS AGGER AGGIE AGGRO AGHAS AGILE

But when i fill an array like following the regex detects.

   arrayWords[0]="apcple";
   arrayWords[1]="appa";
   arrayWords[2]="esjdchc";
   arrayWords[3]="csksjce";
   arrayWords[4]="ce";
   arrayWords[5]="jazz";
   arrayWords[6]="classic";
   arrayWords[7]="sjdheiudc";
   arrayWords[8]="no";
   arrayWords[9]="yes";

I dont understand Y?
PLZ someone help me...

I'm not a regex expert, but it strikes me that your regex is trying to match lower case cheracters, like in the test array, but the data from your file is all upper case.
PS please post in full English without text-speak abbreviations because many of our members have English as a second or third language. and we want everybody to understand.

Some general advice. Don't use StringTokenizer; the Javadocs clearly state that it's a legacy class. Use the split method of the String class instead. Never catch exceptions only to print out something. Make sure you either propagate them, log them to a file/console or at least print the stack trace. The cast to String on line 62 is unnecessary. Calling matches on the String object compiles the passed in pattern for each iteration. If your regex doesn't change, consider creating/compiling a pattern just once and reusing it for different strings.

Regarding your problem, it seems you need to match words which have a c followed by e in a case insensitive manner. The trick is to use character classes i.e. use the pattern as .*[Cc].*[Ee].*. But use of wildcards at the start of string triggers a lot of backtracking so your pattern can be simply reduced to [Cc].*[Ee] along with using the find method of the Matcher class instead of matches. The difference between those two methods is explained here.

This question has already been answered. Start a new discussion instead.