如何使用正则表达式来匹配 b1、b2、..通过 B15,但没有进一步



我在运行MacOS Mojave的Mac上使用Java,Eclipse。似乎这很容易,但我花了 4-5 个小时。需要识别以下字符串:b1, b2, b3, ... b14, b15 .
试过" ^b1[012345]{1}$ | ^b[1-9]?$和其他: (^b1[012345]{1}$) | (^b[1-9]{1}$)

^b(1 | 2| 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | | 13 | 14 | 15){1}$甚至 ^b( '1' | '2' | '3' | '4' | '5' | '6' | '7'... | '15'){1}$

提前非常感谢。

试试这个正则表达式:

bb(?:1[0-5]|[1-9])b

点击查看演示

在 JAVA 中,您需要使用另一个转义

解释:

  • b - 匹配单词边界
  • b - 匹配字母b
  • (?:1[0-5]|d) - 匹配 1,后跟 OR 范围内的数字0-5 匹配 1-9 中的一位数字
  • b - 匹配单词边界

谢谢。 但它没有奏效;我摆弄着它,无法开始工作。 以下内容很冗长,但它有效:

String first = pageText.substring(0, 1);
String rest = pageText.substring(1, pageText.length());
String pattern = "[^0-9]";
Matcher matcher = Pattern.compile(pattern).matcher(rest);
    while (matcher.find()) {
        JOptionPane.showMessageDialog(null,
            "<html>Only b followed by a number between 1 and “
            + “15 in the page number field",
            "Page Number Is Not Recognizable",
            JOptionPane.ERROR_MESSAGE);
        return;
}
int intRest = Integer.parseInt(rest); // string to integer
 if ((intRest > 0 && intRest < 16) && (first.equals("b"))) {
      // valid

相关内容

最新更新