Question

I am using this regex expression:

[Ss]([0-9]+)[][ ._-]*[Ee]([0-9]+)([^\\/]*)$

It throws an exception when I am converting into a pattern:

pattern = Pattern.compile(regExp.get(i));


    05-15 11:16:06.914: E/AndroidRuntime(5268): java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 43:
05-15 11:16:06.914: E/AndroidRuntime(5268): [Ss]([0-9]+)[][ ._-]*[Ee]([0-9]+)([^\\/]*)$
05-15 11:16:06.914: E/AndroidRuntime(5268):                                            ^
05-15 11:16:06.914: E/AndroidRuntime(5268):     at java.util.regex.Pattern.compileImpl(Native Method)
05-15 11:16:06.914: E/AndroidRuntime(5268):     at java.util.regex.Pattern.compile(Pattern.java:400)
05-15 11:16:06.914: E/AndroidRuntime(5268):     at java.util.regex.Pattern.<init>(Pattern.java:383)
05-15 11:16:06.914: E/AndroidRuntime(5268):     at java.util.regex.Pattern.compile(Pattern.java:374)

It works fine when running on a java regex tester. I believe I need to escape some characters here..

Was it helpful?

Solution

2 issues I see here:

  1. You are using an empty character class []
  2. Backslash needs more escaping

You can use this regex:

[Ss]([0-9]+)[ ._-]*[Ee]([0-9]+)([^\\\\/]*)$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top