java - 数学表达式中的字符串匹配 x <= 900 (int, int)
问题描述
我有数字,比如 900。"x <= int (int, int)"
例如,我提取了一个格式为的字符串。"x <= 900 (4, 7)"
我想验证它是否与格式匹配"x <= 900 (int, int)"
如何使用字符串模式匹配器来做到这一点?
到目前为止,我已经尝试了以下方法,结果是错误的。
1) Pattern pattern = Pattern.compile("x <= 900 (\\d, \\d)");
2) Pattern pattern = Pattern.compile("x <= 900 ([0-9], [0-9])");
String expectedString = "x <= 900 (4, 7)";
Matcher m = pattern.matcher(expectedString);
System.out.println(m.matches());
解决方案
您需要使用反斜杠 ( \
) 转义括号,因为它们是正则表达式元字符。要匹配一位或多位数字,请使用\d+
.
Pattern pattern = Pattern.compile("x <= \\d+ \\(\\d+, \\d+\\)");
推荐阅读
- c# - TransactionScope 打破 SqlConnection 池?
- r - Log Log Plot - 如何理解轴
- ruby-on-rails - Codeship 将测试标记为“失败”,即使没有错误
- r - 使用 purrr 中的 pmap 迭代具有时间序列函数的列表列表
- sql - PostgreSQL 基于列的条件计数
- javascript - 合并两个数组然后在javascript中计算它
- c - EOF 无法识别
- python-3.x - 带有设备型号 ID 和设备 ID 的树莓派分段错误的 Google 助手
- ruby-on-rails - Forest Admin 尚未在生产中显示模型,但在开发中显示它
- python - Anaconda3 的 python 找不到 kerberos 凭证缓存