asdascascaedfa
asdascascaedfa

Reputation: 137

Regular expression, reading character by character(java)

Sorry for my English. Read file character by character. I need to output a string that meets our expression, in this case ([a-zA-Z0-9]){0,}. That's how I did it: if a line break occurs, then there is a check on the regular expression. Why does not it work?

UDP code:

    public static void main(String[] args) throws FileNotFoundException, IOException {

        int endWord = wordToFind.length(); 
        int startWord = 0;  
        String myWord = ""; 
        int numbLine = 1; 

        char[] barray = new char[1024]; 
        StringBuilder stringB = new StringBuilder(); 

        Pattern p = Pattern.compile("([a-zA-Z0-9]){0,}");

        try(BufferedReader  reader = new BufferedReader(new InputStreamReader(new FileInputStream("text.txt"))))
        {   
            startTime = System.currentTimeMillis();
            int value;
            while((value = reader.read(barray, 0, barray.length)) != -1) {
                for(int i = 0; i < value; i++) {

                    stringB.append(barray[i]); 

                   if( Character.toString(barray[i]).equals("\n") ) {

                        Matcher m = p.matcher(stringB.toString());

                        if(m.matches()) {
                            finwWordLine = true;
                        }

                        if(finwWordLine) { 
                            System.out.println(numbLine + ": " +stringB.toString()); 
                        }

                        stringB.delete(0, stringB.length()); 
                        finwWordLine = false; 
                        numbLine++; 
                    }
} 

            }

           reader.close();



        }catch(Exception e){
           System.out.println("Error : "+e);
        }
    }

UPD text.txt

one line

two line

three line asd asdas das dasd asd asdasdasd five line

one

asdasd asd asd asd

Upvotes: 2

Views: 200

Answers (1)

Stephan
Stephan

Reputation: 43053

Try this regex instead:

[a-zA-Z0-9]*

Upvotes: 1

Related Questions