How to check if a given Regex is valid?

前端 未结 7 2209
时光取名叫无心
时光取名叫无心 2020-12-05 02:08

I have a little program allowing users to type-in some regular expressions. afterwards I like to check if this input is a valid regex or not.

I\'m w

相关标签:
7条回答
  • 2020-12-05 02:40

    new String().matches(regEx) can be directly be used with try-catch to identify if regEx is valid.

    boolean isValidRegEx = true;
    try {
        new String().matches(regEx);
    } catch(PatternSyntaxException e) {
        isValidRegEx = false;
    }
    
    0 讨论(0)
提交回复
热议问题