Java String.replaceAll() with back reference

后端 未结 5 2129
天涯浪人
天涯浪人 2020-12-14 21:08

There is a Java Regex question: Given a string, if the \"*\" is at the start or the end of the string, keep it, otherwise, remove it. For example:

  1. *
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 21:21

    According to the Javadoc:

    Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

    Your regex: "(^\\*)|(\\*$)|\\*"

    After removing quotes and String escapes: (^\*)|(\*$)|\*

    There are three parts, separated by pipes |. The pipes mean OR, which means that replaceAll() replaces them with the stuff from the second part: $1$2. Essentially, the 1st part >> $1, the second >> $2, the third >> "". Note that "the 1st part" == $1, and so on... So it's not technically replaced.

    1 (^\*) is a capture group (the first). ^ anchors to the string start. \* matches *, but needs the escape \.

    2 (\*$) again, a capture group (2nd one). Difference here is it anchors to the end with $

    3 \* like before, matches a literal *

    The thing you need to understand about regexes is it will always take the first path if it matches. While *s at the beginning and end of the string could be matched by the 3rd part, they match the first or second parts instead.

提交回复
热议问题