java - 正则表达式 - 列出与模式不匹配的字符
问题描述
我有下面的正则表达式,它说明给定的输入是否与模式匹配
String input="The input here @$%/";
String pattern="[a-zA-Z0-9,.\\s]*";
if (!input.matches(pattern)) {
System.out.println("not matched");
}else{
System.out.println("matched");
}
我能知道如何增强它以列出输入中与模式不匹配的字符吗?例如这里@$%/
解决方案
正如anubhava 在评论中已经提到的那样,只需使用input.replaceAll(pattern, "")
.
演示:
class Main {
public static void main(String[] args) {
String input = "The input here @$%/";
String pattern = "[a-zA-Z0-9,.\\s]*";
String nonMatching = input.replaceAll(pattern, "");
System.out.println(nonMatching);
}
}
输出:
@$%/
推荐阅读
- python - InvalidArgumentException:消息:无效参数(会话信息:无头 chrome=88.0.4324.182)?
- powerbi - PowerBI -Total Row 显示最小值,即使我对该列的 Measure 是一个总和?
- swiftui - 在 DatePicker 中调用 .onChange 之前的函数
- php - 从数组输出日期从星期一到星期五
- javascript - WebCrypto JS SHA256 HMAC 不匹配
- java - HttpURLConnection 连接变量未设置值并获取 HTTP 响应代码:401 异常
- arrays - 用函数 C 计算数组的长度
- rust - 如何在 Rust 中打印“BdabePublicKey”?
- javascript - 如果选中复选框,则需要下拉菜单选择
- python - CSV与python multipleindex的比较