Java文本过滤器
对于一个项目,我需要开发een Java应用程序来检查多个部分上的字符串:Java文本过滤器,java,regex,string,Java,Regex,String,对于一个项目,我需要开发een Java应用程序来检查多个部分上的字符串: 首先,检查单词是否包含指定列表中的单词。 请注意: 输入可能包含列表中的一个单词,但字符之间可以有空格或特殊字符以绕过过滤器。在这种情况下,过滤器需要将单词过滤到 这个词可以放在另一个词中。然后,如果指定了before和/或after过滤器,则需要过滤单词 第二个是过滤包含ip地址的文本。 请注意: 输入可能包含ip,其中使用特殊字符或空格绕过筛选器。在这种情况下,筛选器需要将ip地址筛选到 第三个是从文本中过滤
请注意:
- 输入可能包含列表中的一个单词,但字符之间可以有空格或特殊字符以绕过过滤器。在这种情况下,过滤器需要将单词过滤到
- 这个词可以放在另一个词中。然后,如果指定了before和/或after过滤器,则需要过滤单词李>
请注意:
- 输入可能包含ip,其中使用特殊字符或空格绕过筛选器。在这种情况下,筛选器需要将ip地址筛选到
此外,请注意:
- 输入可能包含使用特殊字符或空格绕过筛选器的网址。在这种情况下,筛选器需要将web地址筛选到
public static boolean validateBericht(String msg) {
return validateTransformedBericht(msg);
}
private static boolean validateTransformedBericht(String bericht) {
if (bericht.length() != 0) {
for (String woord : ChatControlList.getChatControlList()
.getWoordenLijst()) {
for (int i = 0; i < (bericht.length() - (woord.length() - 1)); i++) {
if (i == 0 || inTekenLijst(bericht.charAt(i))) {
int index = 0;
for (int j = i; j < bericht.length(); j++) {
if (inTekenLijst(bericht.charAt(j))) {
} else if (bericht.charAt(j) == woord.charAt(index)) {
index++;
} else {
break;
}
if (index == woord.length()) {
if ((bericht.length() - 1) == j
|| inTekenLijst(bericht.charAt(index))) {
return true;
} else {
break;
}
}
}
}
}
}
}
return false;
}
private static boolean inTekenLijst(char teken) {
for (String tekenUitLijst : ChatControlList.getChatControlList()
.getSpecialeTekens()) {
if (tekenUitLijst.equalsIgnoreCase(String.valueOf(teken))
|| String.valueOf(teken).equalsIgnoreCase(" ")) {
return true;
}
}
return false;
}
public静态布尔validateBericht(字符串msg){
返回validateTransformedBericht(msg);
}
私有静态布尔validateTransformedBericht(字符串bericht){
如果(bericht.length()!=0){
for(String-woord:ChatControlList.getChatControlList()
.getWoordenLijst()){
对于(int i=0;i<(bericht.length()-(woord.length()-1));i++){
如果(i==0 | | inTekenLijst(bericht.charAt(i))){
int指数=0;
对于(int j=i;j
有人知道如何用有效的解决方案来解决这个问题吗
Harm在这种情况下,您应该创建两种方法:
- 首先测试字符串是否与搜索的单词匹配
- 第二个用来测试地址的类型
String line = "the wor ld is wonderful";
String search = "wor ld";
String pattern = "(" + search + ")";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Found value: " + m.group(0));
} else {
System.out.println("NO MATCH");
}
public static String testAddress(String address) {
if (address.matches("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
return "IP Address";
} else if (address.matches("^(http\\:\\/\\/|https\\:\\/\\/)?([a-z0-9][a-z0-9\\-]*\\.)+[a-z0-9][a-z0-9\\-]*$")) {
return "Web address";
} else {
return "invalid input";
}
}
这是一个在这种情况下,您应该创建两种方法:
- 首先测试字符串是否与搜索的单词匹配
- 第二个用来测试地址的类型
String line = "the wor ld is wonderful";
String search = "wor ld";
String pattern = "(" + search + ")";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Found value: " + m.group(0));
} else {
System.out.println("NO MATCH");
}
public static String testAddress(String address) {
if (address.matches("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
return "IP Address";
} else if (address.matches("^(http\\:\\/\\/|https\\:\\/\\/)?([a-z0-9][a-z0-9\\-]*\\.)+[a-z0-9][a-z0-9\\-]*$")) {
return "Web address";
} else {
return "invalid input";
}
}
这是一个对于第一部分,您可以去掉所有特殊字符和空格,例如
testString = origString.replaceAll("[- #$%]", ""); //Extend the regex to add your own special characters
…然后搜索单词
containsWord = testString.toLowerCase().contains(badWord);
对于第一部分,您可以去掉所有特殊字符和空格,例如
testString = origString.replaceAll("[- #$%]", ""); //Extend the regex to add your own special characters
…然后搜索单词
containsWord = testString.toLowerCase().contains(badWord);
您能否提供示例字符串以及您试图获得的匹配项?根据您提供的模糊描述,谁会有想法?我从中看到,需要对字符串值进行一些测试。是的,正则表达式可能是一个“好的工作解决方案”的方法。描述会更新一些更多的信息。你能提供示例字符串和你试图获得的匹配项吗?根据你提供的模糊描述,谁会有想法?我从中看到,需要对字符串值进行一些测试。是的,正则表达式可能是一个“好的工作解决方案”的方法。描述会更新一些更多的信息。我将对此进行测试。看起来像是我在找的东西。我要测试一下。看起来像是我在找的东西。