How to use Regex in Java to pattern match?

前端 未结 3 777
长情又很酷
长情又很酷 2020-12-10 20:14

I have read the documentation and various tutorials online but I\'m still confused on how regex works in Java. What I am trying to do is create a function which takes in ar

3条回答
  •  隐瞒了意图╮
    2020-12-10 21:04

    You will need to use Java's character class intersection operator inside a character class, otherwise it literally matches &&. Btw, your first character class from A to (lowercase) z also includes [\]^_, which you certainly do not want; and you misspelled "Patter.complile".

    Also, matches()

    Attempts to match the entire region against the pattern.

    So you either need to use find() instead or pad the expression with .*.

    public boolean checkString(String arg) {
        return Pattern.compile("[[a-zA-Z]&&[^MDCLXVIivxlcdm]]").matcher(arg).find();
    }
    

提交回复
热议问题