Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java regex, matching math operators

Tags:

java

regex

I am trying to write a regex to match math operators to a char. I've tried a bunch of different things but i keep getting:

Syntax error on tokens, Expression expected instead

my code looks like this:

public static void readMath(char c) {
        if(c == [+\-*/]) {
            // do some stuff
        }
    }

i've tried escaping different things etc etc. i can't seem to get it to work.

like image 392
Ted Avatar asked Jul 21 '26 02:07

Ted


1 Answers

public static void readMath( char c ) {
    if ( String.valueOf( c ).matches( "[-+*/]" ) ) {
        // do stuff
    }
}
like image 119
MikeM Avatar answered Jul 23 '26 17:07

MikeM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!