gpt4 book ai didi

java - 如何在java中检查匹配模式

转载 作者:行者123 更新时间:2023-12-01 11:00:31 24 4
gpt4 key购买 nike

我想匹配 "sec 30-31" 这样的模式。但它对我不起作用。

我正在使用如下所示的匹配模式。

private boolean checkConstraint(String inputValue) {
inputValue = inputValue.trim();
if (inputValue.matches("[\\>][\\s]*[\\d]*") || inputValue.matches("[\\<][\\s]*[\\d]*") || inputValue.matches("[\\d]*[\\s]*[-][\\s]*[\\d]*")
|| inputValue.matches("[\\=][\\s]*[\\d]*") || inputValue.matches("[\\=][\\s]*[\\w]*") || inputValue.matches("[\\d]*")
|| inputValue.matches("[\\w]*") || inputValue.matches("[\\d]*[\\s]*[|][\\s]*[\\d]*")
|| inputValue.matches("[\\w]*[\\s]*[|][\\s]*[\\w]*") || inputValue.matches("[\\d]*[\\,][[\\s]*\\d\\,]*")
|| inputValue.matches("[\\w]*[\\,][[\\s]*\\w\\,]*") || inputValue.matches("[\\d]*[\\s]*[\\?]")
|| inputValue.matches("[\\w]*[\\s]*[\\?]") || inputValue.matches("[\\d]*[\\s]*[\\*]") || inputValue.matches("[\\w]*[\\s]*[\\*]")
|| inputValue.matches("[\\w]*[\\s]*[\\w\\,]*") || inputValue.matches("[\\s]*[\\%]*[\\s]*[\\w]*[\\s]*")
|| inputValue.matches("[\\s]*[\\w]*[\\s]*[\\%][\\s]*") || inputValue.matches("[\\s]*[\\%]*[\\s]*[\\w]*[\\s]*[\\%][\\s]*")
|| inputValue.matches("[\\w]*[\\s]*[-][\\s]*[\\w]*") || inputValue.matches("[\\w]*[\\s]*[.][\\s]*[\\w]*")
|| inputValue.matches("[\\w]*[\\s]*['][\\s]*[\\w]*") || inputValue.matches("[[\\w]*[\\s]*[\\w]*[\\s]*]*")) {
return true;
}
return false;
}

我想匹配字符串“sec 30-31”,当我匹配它的返回 false 时,它​​将返回 true inputValue.matches("[\\w]*[\\s]*[-][\\s]*[\\w]*")

任何人都可以帮助我。

谢谢

西坦苏

最佳答案

你的正则表达式

inputValue.matches("[\\w]*[\\s]*[-][\\s]*[\\w]*")

不会匹配 sec 30-31,因为它匹配 字母数字+空格+-+空格+字母数字。如您所见,没有数字的位置。

您需要将另一个 matches() 添加到您的“管道”中:

inputValue.matches("[a-zA-Z]+\\s+\\d+-\\d+")

参见IDEONE demo返回true

这里,

  • [a-zA-Z]+ - 匹配 1 个或多个拉丁字母
  • \\s+ - 匹配 1 个或多个空格
  • \\d+ - 匹配 1 个或多个数字
  • - - 匹配文字连字符
  • \\d+ - 匹配 1 个或多个数字。

关于java - 如何在java中检查匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33364861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com