I have a string like message = "ASDF rfghy !@#$ :>< "
I want to check this string contain ASCII value between 0 to 255 using regex(java).
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
The isascii() function returns a boolean value where True indicates that the string contains all ASCII characters and False indicates that the string contains some non-ASCII characters.
The regular expression represents all printable ASCII characters. ASCII code is the numerical representation of all the characters and the ASCII table extends from char NUL (Null) to DEL . The printable characters extend from CODE 32 (SPACE) to CODE 126 (TILDE[~]) .
In order to find the ASCII value of a character, simply assign the character to a new variable of integer type. Java automatically stores the ASCII value of that character inside the new variable.
You can try the regex:
"^\\p{ASCII}*$"
In regex, \x00
matches the hex character 00
and character classes work on these. So you can do:
/^[\x00-\x7F]+$/
to match a string of one or more ascii values.
Just use this code to do this check:
System.out.println("Matches: " + message.matches("[\u0000-\u00FF]+"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With