java regex pattern unclosed character class

后端 未结 2 1040
时光说笑
时光说笑 2020-11-28 16:38

I need some help. Im getting:

Caused by: java.util.regex.PatternSyntaxException: Unclosed character class near index 24
^[a-zA-Z└- 0-9£µ /.\'-\\]*$
                  


        
2条回答
  •  广开言路
    2020-11-28 17:22

    It is hard to say what are you trying to achieve, but I can see few strange things in your regex:

    1. you have opened class of characters but never closed it. Instead you used \\] which makes ] normal character.
      • If you want to include ] in your characters class then you need additional ] at the end, like "^[a-zA-Z\300-\3770-9\u0153\346 \u002F.'-\\]]*$"
      • if you want to include \ in your characters class then you need to use \\\\ version, because you need to escape its special meaning two times, in regex engine, and in Javas String
    2. you used - with ('-\\]) which in character class is used to specify range of characters like a-z or A-Z. To escape its special meaning you need to use \\-

提交回复
热议问题