I want to remove a string at the beginning of another.
Example:
Begin | Removed | Final |
---|---|---|
"\n123 : other" | Yes | other |
"123 : other" | No | Error |
"\n4 : smth" | Yes | smth |
"123 : a" | No | Error |
Thanks to Regexr, I made this regex:
/[\\][n](?:[0-9]*)[ ][:]/g
I tried to use it in Java. With string.matches()
I don't have any error. But, with string.replaceFirst
, I have this error :
java.util.regex.PatternSyntaxException: Unclosed character class near index 24
/[\][n](?:[0-9]*)[ ][:]/g
^
What I tried else ?
- Remove
/
at begin and/g
at end. - Add
(pattern)
(such as visible in regex here) - Multiple others quick way, but I the error is always at the last char of my regex
My full code:
String str = "\n512 : something"; // I init my string
if(str.matches("/[\\][n](?:[0-9]*)[ ][:]/g"))
str = str.replaceFirst("/[\\][n](?:[0-9]*)[ ][:]/g", "");
How can I fix it ? How can I solve my regex pattern ?