How to escape a square bracket for Pattern compilation

后端 未结 4 1482
萌比男神i
萌比男神i 2020-12-03 10:16

I have comma separated list of regular expressions:

.{8},[0-9],[^0-9A-Za-z ],[A-Z],[a-z]

I have done a split on the comma. Now I\'m trying

4条回答
  •  春和景丽
    2020-12-03 10:41

    Pattern.compile() likes square brackets just fine. If you take the string

    ".{8},[0-9],[^0-9A-Za-z ],[A-Z],[a-z]"
    

    and split it on commas, you end up with five perfectly valid regexes: the first one matches eight non-line-separator characters, the second matches an ASCII digit, and so on. Unless you really want to match strings like ".{8}" and "[0-9]", I don't see why you would need to escape anything.

提交回复
热议问题