What regex would match any ASCII character in java?
I\'ve already tried:
^[\\\\p{ASCII}]*$
but found that it didn\'t match lots of
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
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
For JavaScript it'll be /^[\x00-\x7F]*$/.test('blah')
I have never used \\p{ASCII}
but I have used ^[\\u0000-\\u007F]*$
The first try was almost correct
"^\\p{ASCII}*$"