Java Regexp匹配ASCII字符
什么正则表达式匹配java中的任何ASCII字符?
我已经尝试过了:
^[\\p{ASCII}]*$
但发现它与我想要的很多东西(如空格,括号等等)不匹配。 我希望避免以下列格式明确列出所有127个ASCII字符:
^[a-zA-Z0-9!@#$%^*(),.~`[]{}\\/+=-\\s]*$
我从未使用过\\p{ASCII}
但我使用了^[\\u0000-\\u007F]*$
第一次尝试几乎是正确的
"^\\p{ASCII}*$"
对于JavaScript,它将是/ /^[\x00-\x7F]*$/.test('blah')
如果您只想要可打印的ASCII字符,可以使用^[ -~]*$
– 即空格和波浪号之间的所有字符。
https://en.wikipedia.org/wiki/ASCII#ASCII_printable_code_chart
我想有关从原始字符串中获取ASCII字符的问题,该字符串具有ASCII和特殊字符…
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; }
上面的程序将删除非ascii字符串并返回字符串。 感谢@Oleg Pavliv的模式。
例如:
raw = + 919986774157
asciiString = +919986774157