1

我正在尝试检查字符串是否以点和 2 或 3 个字符结尾。我使用的正则表达式是:

[.][a-z0-9A-Z][a-z0-9A-Z][a-z0-9A-Z]$

字符串示例:qsdgfdssdh.nfo

它应该返回 true,但它总是返回 false。

你能帮助我吗?

谢谢

4

1 回答 1

1

String#matches函数将模式应用于整个字符串。所以以下应该工作:

String input = "qsdgfdssdh.nfo";
if (input.matches(".*\\.[0-9A-Za-z]{3}")) {
    System.out.println("match");
}

如果你想知道你当前的模式会匹配什么String#matches,它会匹配.nfo

String input = ".nfo";
if (input.matches("\\.[0-9A-Za-z]{3}")) {
    System.out.println("match");
}

演示

于 2018-05-12T06:13:28.977 回答