我有以下的话:
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
is\s?(this|that|it)\s?true\s[?]?
^real$
^reall[y]*[\s]?[?]*$
对于每个字符串,我必须搜索字符串中是否存在这些单词。
最好的办法是什么?
我尝试过使用:
wh[a]*[t]*[?!][?]*
但它很慢。有更好的方法吗?
答案 0 :(得分:-1)
如果您在很多字符串上使用相同的正则表达式,可以尝试使用re.compile
来节省时间。