用于限制位数的正则表达式

时间:2014-10-01 07:53:43

标签: regex

我正在尝试编写一个只与 qtr1 qtr2 qtr3 qtr4 在下面的正则表达式[q|qtr|qtrs|quarter]+[1-4]的帮助下,问题是如果我问这样的事情" Ficoscore for 2005年第21期"在Q和21之间添加一个空格,即" Ficoscore for Q 21 2005"这无效。

  String regEx = "([q|qtr|qtrs|quarter]+[1-4])";
  Pattern pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
  Matcher matcher = pattern.matcher(userQuerySentence);
  System.out.println(matcher.matches());
  while (matcher.find()) {
     String quarterString = matcher.group();
     userQuerySentence = userQuerySentence.replaceAll(quarterString,
              (quarterString.substring(0, quarterString.length() - 1) + " " + quarterString.substring(quarterString
                       .length() - 1)));
  }

1 个答案:

答案 0 :(得分:1)

[q|qtr|qtrs|quarter]是一个字符类,我想你想要(q|qtr|qtrs|quarter)

String regEx = "(?i)\\b((?:q(?:trs?|uarter)?)[1-4])\\b";