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
  • 49,592
  • 20
  • 90
  • 140
Jay Patel
  • 105
  • 2
  • 5

3 Answers3

5

You can try the regex:

"^\\p{ASCII}*$"
codaddict
  • 429,241
  • 80
  • 483
  • 523
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,381
  • 2
  • 34
  • 55
2

Just use this code to do this check:

System.out.println("Matches: " + message.matches("[\u0000-\u00FF]+"));
anubhava
  • 713,503
  • 59
  • 514
  • 593