Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regexp to Match ASCII Characters

Tags:

java

regex

What regex would match any ASCII character in java?

I've already tried:

^[\\p{ASCII}]*$

but found that it didn't match lots of things that I wanted (like spaces, parentheses, etc...). I'm hoping to avoid explicitly listing all 127 ASCII characters in a format like:

^[a-zA-Z0-9!@#$%^*(),.<>~`[]{}\\/+=-\\s]*$
like image 871
David Avatar asked Feb 21 '11 21:02

David


3 Answers

The first try was almost correct

"^\\p{ASCII}*$"
like image 191
Oleg Pavliv Avatar answered Oct 23 '22 20:10

Oleg Pavliv


I have never used \\p{ASCII} but I have used ^[\\u0000-\\u007F]*$

like image 42
Bala R Avatar answered Oct 23 '22 21:10

Bala R


If you only want the printable ASCII characters you can use ^[ -~]*$ - i.e. all characters between space and tilde.

https://en.wikipedia.org/wiki/ASCII#ASCII_printable_code_chart

like image 1
Raniz Avatar answered Oct 23 '22 21:10

Raniz