简体   繁体   中英

Regular expression starts with string and match all type of symbols, character, numbers

I tried with

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();
    }
}

It returns false, i want [] should be there in expression, i want expression starts with string and accepts all symbols, numbers,everything, with [], {}, () also

Try with the following regex :

"^[a-zA-Z][a-zA-Z0-9&\\-()$#@!^*=|'{}:.%_+?`~<>\\[\\]]*$"

[ and ] need to be escaped within your custom character class ( https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM