What exactly does a Regex created with Regex.fromLiteral() match?

后端 未结 2 786
遇见更好的自我
遇见更好的自我 2020-12-11 18:42

I\'ve created a very simple match-all Regex with Regex.fromLiteral(\".*\").

According to the documentation: \"Returns a literal regex for the specified

2条回答
  •  借酒劲吻你
    2020-12-11 19:28

    The Regex.fromLiteral() instantiates a regex object while escaping the special regex metacharacters. The pattern you get is actually \.\*, and since you used matches() that requires a full string match, you can only match a .* string with it (with find() you could match it anywhere inside a string).

    See the source code:

    public fun fromLiteral(literal: String): Regex = Regex(escape(literal))

提交回复
热议问题