Java Regexp to Match ASCII Characters

前端 未结 5 1107
甜味超标
甜味超标 2020-12-05 08:19

What regex would match any ASCII character in java?

I\'ve already tried:

^[\\\\p{ASCII}]*$

but found that it didn\'t match lots of

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 09:00

    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

提交回复
热议问题