Java 匹配器结果-正则表达式
有没有办法知道Matcher出了什么问题?在这种情况下,不允许使用“!”,从正则表达式中可以看到。我想知道一个结果,类似“Java 匹配器结果-正则表达式,java,regex,Java,Regex,有没有办法知道Matcher出了什么问题?在这种情况下,不允许使用“!”,从正则表达式中可以看到。我想知道一个结果,类似“”!”的内容是不允许的”您可以先测试正则表达式是否匹配(使用函数没有理由添加^和$),然后如果不匹配,您可以处理一些可能的情况: final String regex = "^[A-Za-z0-9]{1,12}$"; final Pattern pattern = Pattern.compile(regex); System.out.println(pa
”!”的内容是不允许的
”您可以先测试正则表达式是否匹配(使用函数没有理由添加^
和$
),然后如果不匹配,您可以处理一些可能的情况:
final String regex = "^[A-Za-z0-9]{1,12}$";
final Pattern pattern = Pattern.compile(regex);
System.out.println(pattern.matcher("hi!").?);
您可以根据模式将所有字符替换为
“
,这将为您提供不允许的字符。然而,这里有一个陷阱:我使用的模式没有行的开头(^
)和行的结尾($
),这是我将考虑和更新的内容
演示:
public void complianceCheck(String inputString) {
final String regex = "[A-Za-z0-9]{1,12}";
if (!Pattern.compile(regex).matcher(inputString).matches()) {
Matcher matcher = Pattern.compile(regex).matcher(inputString);
if (matcher.find()) {
String match = matcher.group(0);
if (Pattern.compile("^" + regex).matcher(inputString).find())
System.out.printf("'%s' shouldn't be followed by '%s'\n", match, inputString.replaceFirst("^" + regex, ""));
else if (Pattern.compile(regex + "$").matcher(inputString).find())
System.out.printf("'%s' shouldn't be preceded by '%s'\n", match, inputString.replaceFirst(regex + "$", ""));
else
System.out.printf("%s shouldn't be between something else\n", match);
}
else System.out.println("No match");
}
else System.out.println("OK");
}
@Test
public void test() {
complianceCheck("hi!"); // this will print "'hi' shouldn't be followed by '!'"
complianceCheck("...hi"); // this will print "'hi' shouldn't be preceded by '...'"
complianceCheck("...hi!"); // this will print "'hi' shouldn't be between something else"
complianceCheck("..."); // this will print "No match"
complianceCheck("hi"); // this will print "OK"
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
final String regex = "[A-Za-z0-9]{1,12}";
String str = "hi!&";
String remaining = str.replaceAll(regex, "");
System.out.println("The following characters are not allowed: " + Arrays.toString(remaining.split("")));
}
}
输出:
public void complianceCheck(String inputString) {
final String regex = "[A-Za-z0-9]{1,12}";
if (!Pattern.compile(regex).matcher(inputString).matches()) {
Matcher matcher = Pattern.compile(regex).matcher(inputString);
if (matcher.find()) {
String match = matcher.group(0);
if (Pattern.compile("^" + regex).matcher(inputString).find())
System.out.printf("'%s' shouldn't be followed by '%s'\n", match, inputString.replaceFirst("^" + regex, ""));
else if (Pattern.compile(regex + "$").matcher(inputString).find())
System.out.printf("'%s' shouldn't be preceded by '%s'\n", match, inputString.replaceFirst(regex + "$", ""));
else
System.out.printf("%s shouldn't be between something else\n", match);
}
else System.out.println("No match");
}
else System.out.println("OK");
}
@Test
public void test() {
complianceCheck("hi!"); // this will print "'hi' shouldn't be followed by '!'"
complianceCheck("...hi"); // this will print "'hi' shouldn't be preceded by '...'"
complianceCheck("...hi!"); // this will print "'hi' shouldn't be between something else"
complianceCheck("..."); // this will print "No match"
complianceCheck("hi"); // this will print "OK"
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
final String regex = "[A-Za-z0-9]{1,12}";
String str = "hi!&";
String remaining = str.replaceAll(regex, "");
System.out.println("The following characters are not allowed: " + Arrays.toString(remaining.split("")));
}
}
正则表达式正在匹配由一组有限字符组成的任何字符串,并且具有最大长度 您可以通过其他方式轻松检查长度。对于不匹配的字符,只需测试相反的字符
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception
{
System.out.println(test(“hello!”);
System.out.println(test(“hello”);
}
公共静态布尔测试(字符串输入){
if(input.length()<1){
System.err.println(“太短”);
返回false;
}
if(input.length()>12){
System.err.println(“太长”);
返回false;
}
最终模式=模式。编译(“[^A-Za-z0-9]”);
匹配器m=模式匹配器(输入);
布尔拒绝=假;
while(m.find()){
System.err.println(m.group(0)+“不允许!”);
拒绝=正确;
}
返回!拒绝;
}
问题是针对任何正则表达式还是仅针对此特定正则表达式?