Android | Syntax Error in regexp pattern

前端 未结 3 1590
猫巷女王i
猫巷女王i 2020-12-21 09:00

I am using \\\\.(.*)} regex pattern to search a specific string in my Android Test Project. when i am using this regex to check on online available tools the re

相关标签:
3条回答
  • 2020-12-21 09:48

    I was facing the same problem. I just surround it with a

     try{
    
     }catch (PatternSyntaxException e) {
               e.printStackTrace();
            }
    

    and it worked.

    0 讨论(0)
  • 2020-12-21 09:53

    You need to escape } as @Rohit Jain said:

    String regex = "\\.(.*)\\}";
    

    Your regex does work in java, but it fails on android for some reason.

    0 讨论(0)
  • 2020-12-21 10:02

    You can try escaping your }: -

    "\\.(.*)\\}"  // escaping `}` not needed in Java
    

    I have no idea why it doesn't work in android, but in Java it works fine without escaping it.

    However, if you are using an opening curly braces, then even in Java you would need to escape it: -

    "\\.(.*)\\{"  // escaping `{` needed even in Java
    
    0 讨论(0)
提交回复
热议问题