簡體   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