java regex pattern unclosed character class

浪尽此生 提交于 2019-11-26 23:17:27

Assuming that you want to match \ and - and not ]:

Pattern pattern = Pattern.compile("^[a-zA-Z\300-\3770-9\u0153\346 \u002F.'\\\\-]*$");

You need to double escape your backslashes, as \ is also an escape character in regex. Thus \\] escapes the backslash for java but not for regex. You need to add another java-escaped \ in order to regex-escape your second java-escaped \.

So \\\\ after java escaping becomes \\ which is then regex escaped to \.

Moving - to the end of the sequence means that it is used as a character, instead of a range operator as pointed out by Pshemo.

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 \\-
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!