【发布时间】:2016-02-28 22:12:21
【问题描述】:
我需要检查一个单独的类中的回文,但忽略非字母字符。例如,如果雷达写成 r,a,d,a,r
,它仍然是合格的我相信我可以使用正则表达式,但我不知道如何。
这是我目前所拥有的,
public static boolean isNonAlpha(char c) {
return (c == '-' || c == '.' || c == ' ' || c == ')' || c == '(') || c == '<' || c == '>' || c == ',';
}
public static String checkInput(String test){
int startChar = 0;
int endChar = test.length() - 1;
while (startChar < endChar) {
if (test.charAt(startChar) != test.charAt(endChar)) {
System.out.println("Your word is not a palindrome.");
System.exit(0);
} else {
if (test.charAt(startChar) == test.charAt(endChar))
startChar++;
endChar--;
}
}
System.out.println("Your word is indeed a palindrome.");
return test;
}
我不知道如何合并我的 isNonAlpha 方法,或者如何使用正则表达式
【问题讨论】:
-
标准正则表达式无法检测回文,因为它们需要记忆(最高无限制)以前接受的内容。 stackoverflow.com/questions/233243/… 但是该页面上有一些实现
-
虽然在某些语言中显示的 Java 正则表达式实现中是可能的,但从技术上讲它们不是常规的
标签: java regex string palindrome