[英]Regular expression starts with string and match all type of symbols, character, numbers
我嘗試過
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,我希望[]應該存在於表達式中,我希望表達式以字符串開頭,並接受所有符號,數字和所有內容,並帶有[],{},()
嘗試使用以下正則表達式:
"^[a-zA-Z][a-zA-Z0-9&\\-()$#@!^*=|'{}:.%_+?`~<>\\[\\]]*$"
[和]需要在您的自定義字符類中轉義( https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.