Java split on ^ (caret?) not working, is this a special character?

前端 未结 5 1635
醉酒成梦
醉酒成梦 2020-11-30 13:33

In Java, I am trying to split on the ^ character, but it is failing to recognize it. Escaping \\^ throws code error.

Is this a special char

5条回答
  •  离开以前
    2020-11-30 13:59

    The ^ is a special character in Java regex - it means "match the beginning" of an input.

    You will need to escape it with "\\^". The double slash is needed to escape the \, otherwise Java's compiler will think you're attempting to use a special \^ sequence in a string, similar to \n for newlines.

    \^ is not a special escape sequence though, so you will get compiler errors.

    In short, use "\\^".

提交回复
热议问题