java - Regex of base classes -


i trying create hexadecimal calculator have problem regex.

basically, want string accept 0-9, a-e, , special characters +-*_

my code keeps returning false no matter how change regex, , adding asterisk giving me patternsyntaxexception error.

public static void main(string[] args) {      string input = "1a_16+2b_16-3c_16*4d_16";      string regex = "[0-9a-e+-_]";      system.out.println(input.matches(regex));  } 

also whenever add * part of regex gives me error:

exception in thread "main" java.util.regex.patternsyntaxexception: illegal character range near index 9 [0-9a-e+-*_]+          ^ 

you need match more 1 character regex. stands match 1 character.

to match 1 or more characters add + end of regex

[0-9a-e+-_]+ 

also match * add star in brackets final regex

[0-9a-e+\\-_*]+ 

you need escape - otherwise regex thinks want accept character between + , _ not want.


Comments