-2

I'm new to regular expressions and StringTokenizer, and I'm getting a syntax error whenever I put this regex in matches:

while ((line = br.readLine()) != null) { 
    StringTokenizer stringTokenizer = new StringTokenizer(line, "\n");

    while (stringTokenizer.hasMoreElements()) {
        String function = stringTokenizer.nextElement().toString();

        if (function.matches(\\s*(unsigned int|float|int|char|void|double)(\\s)+[a-zA-Z_](\\w)*(\\s)*\\((\\s)*((((unsigned int|float|int|char|double)(\\s)*,(\\s)*)*((unsigned int|float|int|char|double)(\\s)*))|(\\s)*|(void)(\\s)*)\\)(\\s)*\\;)) {
            System.out.println("VALID - ");
        }
    }
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Pau
  • 55
  • 1
  • 3
  • 7
  • 3
    Maybe a copy and paste error, but it could be that you're missing the quotes around your regex to make it a `String`. – Brian Jan 28 '13 at 02:10
  • FYI from the [docs](http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html): "`StringTokenizer` is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the `split` method of `String` or the java.util.regex package instead." – Paul Bellora Jan 28 '13 at 02:32
  • Must be lack of sleep, but thanks Brian! It works now -.- The tutorial I was reading online didn't place quotations and I don't know why it didn't occur to me that t needed some. Cheers! – Pau Jan 28 '13 at 03:55
  • 1
    @Brian You should post your comment as an answer so it can be accepted. – Jim Garrison Jan 28 '13 at 09:01

3 Answers3

0

Maybe a copy and paste error, but it could be that you're missing the quotes around your regex to make it a String. For example:

while ((line = br.readLine()) != null) { 
    StringTokenizer stringTokenizer = new StringTokenizer(line, "\n");

    while (stringTokenizer.hasMoreElements()) {
        String function = stringTokenizer.nextElement().toString();

        if (function.matches("\\s*(unsigned int|float|int|char|void|double)(\\s)+[a-zA-Z_](\\w)*(\\s)*\\((\\s)*((((unsigned int|float|int|char|double)(\\s)*,(\\s)*)*((unsigned int|float|int|char|double)(\\s)*))|(\\s)*|(void)(\\s)*)\\)(\\s)*\\;")) {
            System.out.println("VALID - ");
        }
    }
}
Brian
  • 17,079
  • 6
  • 43
  • 66
0

matches method takes String as method argument... So, enclose your regex withing quoutes " " like this:

function.matches("\\s*(unsigned int|float|int|char|void|double)(\\s)+[a-zA-Z_](\\w)*(\\s)*\\((\\s)*((((unsigned int|float|int|char|double)(\\s)*,(\\s)*)*((unsigned int|float|int|char|double)(\\s)*))|(\\s)*|(void)(\\s)*)\\)(\\s)*\\;)")
exexzian
  • 7,782
  • 6
  • 41
  • 52
0

enclose your string withing quotes " ", And note that when using the operator "or" (|) for two consecutive words, we should use them in parenthesis ((unsigned int) | float)


while ((line = br.readLine()) != null) { 
StringTokenizer stringTokenizer = new StringTokenizer(line, "\n");

while (stringTokenizer.hasMoreElements()) {
    String function = stringTokenizer.nextElement().toString();

    if (function.matches("\\s*((unsigned int)|float|int|char|void|double)(\\s)+[a-zA-Z_](\\w)*(\\s)*\\((\\s)*((((unsigned int|float|int|char|double)(\\s)*,(\\s)*)*((unsigned int|float|int|char|double)(\\s)*))|(\\s)*|(void)(\\s)*)\\)(\\s)*\\;")) {
        System.out.println("VALID - ");
    }
}

}

Edgard Leal
  • 2,592
  • 26
  • 30