How to replace brackets in strings

前端 未结 4 501
孤城傲影
孤城傲影 2021-01-14 22:44

I have a list of strings that contains tokens.
Token is:

{ARG:token_name}.

I also have hash map of tokens, where key is th

4条回答
  •  旧时难觅i
    2021-01-14 23:37

    As others already said, { is a special character used in the pattern (} too). You have to escape it to avoid any confusion.

    Escaping those manually can be dangerous (you might omit one and make your pattern go completely wrong) and tedious (if you have a lot of special characters). The best way to deal with this is to use Pattern.quote()


    Related issues:

    • How to escape a square bracket for Pattern compilation
    • How to escape text for regular expression in Java

    Resources:

    • Oracle.com - JavaSE tutorial - Regular Expressions

提交回复
热议问题