繁体   English   中英

我如何使用正则表达式来匹配表达式,例如b1,b2,…到b15,但没有其他内容?

[英]how do I use regex to match expressions such as b1, b2, … thru b15, but no further?

我在运行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,后跟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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM