5

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

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

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Jay Patel
  • 105
  • 2
  • 5

3 Answers3

5

You can try the regex:

"^\\p{ASCII}*$"
codaddict
  • 445,704
  • 82
  • 492
  • 529
3

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.

Dan
  • 10,531
  • 2
  • 36
  • 55
2

Just use this code to do this check:

System.out.println("Matches: " + message.matches("[\u0000-\u00FF]+"));
anubhava
  • 761,203
  • 64
  • 569
  • 643