Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASCII char check using regex(java)

Tags:

java

regex

ascii

I have a string like message = "ASDF rfghy !@#$ :>< "

I want to check this string contain ASCII value between 0 to 255 using regex(java).

like image 329
Jay Patel Avatar asked Dec 13 '11 06:12

Jay Patel


People also ask

How do you check special characters in regex?

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).

How do I check if a string contains ASCII characters?

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.

Does regex use ASCII?

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[~]) .

How do I get the ASCII value of a character Java?

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.


3 Answers

You can try the regex:

"^\\p{ASCII}*$"
like image 75
codaddict Avatar answered Sep 21 '22 07:09

codaddict


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.

like image 28
Dan Avatar answered Sep 18 '22 07:09

Dan


Just use this code to do this check:

System.out.println("Matches: " + message.matches("[\u0000-\u00FF]+"));
like image 29
anubhava Avatar answered Sep 20 '22 07:09

anubhava