16

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]*$
David
  • 215
  • 1
  • 3
  • 5

5 Answers5

34

The first try was almost correct

"^\\p{ASCII}*$"
Oleg Pavliv
  • 20,462
  • 7
  • 59
  • 75
  • Although I would use `"^\\p{ASCII}+$"` so as to not match the empty string, but that might be philosophical... :) – David Jan 19 '16 at 09:05
10

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

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Should there really be two slashes before the u ? i.e. isn't `^[\u0000-\u007F]*$` correct? – Nic Cottrell Apr 14 '15 at 12:43
  • 1
    I tried, single slash works as well. Normally you need double slash because it's an escape command. By the way, I had problems with an String because it has chars from the extended ASCII, but `\\p{ASCII}` is only the standard. For extended ASCII you can use `^[\\u0000-\\u00FE]*$` (`FE` instead of `7F`) – Pascal Schneider Feb 05 '16 at 09:02
  • Why FE, and not FF? – Ingo Schalk-Schupp Sep 02 '19 at 16:43
2

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

Raniz
  • 10,882
  • 1
  • 32
  • 64
1

For JavaScript it'll be /^[\x00-\x7F]*$/.test('blah')

Flexo
  • 87,323
  • 22
  • 191
  • 272
catamphetamine
  • 4,489
  • 30
  • 25
0

I think question about getting ASCII characters from a raw string which has both ASCII and special characters...

public String getOnlyASCII(String raw) {
    Pattern asciiPattern = Pattern.compile("\\p{ASCII}*$");
    Matcher matcher = asciiPattern.matcher(raw);
    String asciiString = null;
    if (matcher.find()) {
        asciiString = matcher.group();
    }
    return asciiString;
}

The above program will remove the non ascii string and return the string. Thanks to @Oleg Pavliv for pattern.

For ex:

raw = ��+919986774157

asciiString = +919986774157

arulraj.net
  • 4,579
  • 3
  • 36
  • 37