我试过
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Rejex {
public static final String REGEX_CONSTANT="^[a-zA-Z][a-zA-Z0-9&\\-()$#@!^*=|'{}:.%_+?`~<>[]]*$";
public static void main(String[] args) {
System.out.println(validateRegex(REGEX_CONSTANT,"dd[]"));
}
public static boolean validateRegex(String regex, String value)
{
Pattern patternObjects = Pattern.compile(regex);
Matcher matcherObject = patternObjects.matcher(value);
return matcherObject.matches();
}
}
它返回false,我希望[]应该在表达式中,我希望表达式以字符串开头并接受所有符号,数字,所有内容,[],{},()也
答案 0 :(得分:0)
尝试使用以下正则表达式:
"^[a-zA-Z][a-zA-Z0-9&\\-()$#@!^*=|'{}:.%_+?`~<>\\[\\]]*$"
[和]需要在自定义字符类(https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html)
中进行转义