如何匹配多个正则表达式之一?

时间:2017-04-30 20:41:50

标签: python regex python-3.x

我有以下的话:

  1. public void menuOptionOne() { counter = 0; // set counter to 0 do { try { if (counter == 0) { System.out.println("Please input the customer's name: "); input.nextLine(); customerName = input.nextLine(); matcher = pattern.matcher(customerName); if (!matcher.find()) { counter++; throw new Exception("Try again. (Incorrect input: name must contain only letters)"); } } else if (counter > 0) { System.out.println("Please input the customer's name: "); customerName = input.nextLine(); matcher = pattern.matcher(customerName); if (!matcher.find()) { throw new Exception("Try again. (Incorrect input: name must contain only letters)"); } } continueInput = false; } catch (Exception ex) { System.out.println(ex.toString()); } } while (continueInput); continueInput = true; // reset the continue input value
  2. is\s?(this|that|it)\s?true\s[?]?
  3. ^real$
  4. ^reall[y]*[\s]?[?]*$
  5. 对于每个字符串,我必须搜索字符串中是否存在这些单词。

    最好的办法是什么?

    我尝试过使用:

    wh[a]*[t]*[?!][?]*

    但它很慢。有更好的方法吗?

1 个答案:

答案 0 :(得分:-1)

如果您在很多字符串上使用相同的正则表达式,可以尝试使用re.compile来节省时间。

相关问题