Regular Expression Program

Posted by david robers on Stack Overflow See other posts from Stack Overflow or by david robers
Published on 2011-01-17T14:48:36Z Indexed on 2011/01/17 14:53 UTC
Read the original article Hit count: 144

Filed under:
|
|

Hi I have the following text:

SMWABCCA
ABCCAEZZRHM
NABCCAYJG 
XABCCA
ABCCADK
ABCCASKIYRH 
ABCCAKY 
PQABCCAK    
ABCCAKQ 

This method takes a regex in out by the user and SHOULD print out the Strings it applies to but seems to print out something completely different:

private void matchIt(String regex) {
        Pattern p = Pattern.compile(regex);
        Matcher m = null;        
        boolean found = false;
        for(int i = 0; i < data.length; i++){
            m = p.matcher(data[i]);
            if(m.find()){
                out.println(data[i]);
                found = true;
            }
        }

        if(!found){
            out.println("Pattern Not Found");
        }
    }

When inputting "[C]"

It outputs:

SMWABCCA
ABCCAEZZRHM
NABCCAYJG
XABCCA
ABCCADK
ABCCASKIYRH
ABCCAKY
PQABCCAK
ABCCAKQ

Any ideas why? I think I'm using m.find() improperly...

© Stack Overflow or respective owner

Related posts about java

Related posts about regex