Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy: Why do I need to double escape square brackets?

Tags:

groovy

Why doesn't this work?

"hello[world".split("\[")

and this does:

"hello[world".split("\\[")
like image 914
GilaH Avatar asked Jul 08 '12 13:07

GilaH


People also ask

Do square brackets need to be escaped?

Use square brackets as escape characters for the percent sign, the underscore, and the left bracket. The right bracket does not need an escape character; use it by itself. If you use the hyphen as a literal character, it must be the first character inside a set of square brackets.

How do you pass square brackets in regex?

How do you use square brackets in regex? Use square brackets ( [] ) to create a matching list that will match on any one of the characters in the list. Virtually all regular expression metacharacters lose their special meaning and are treated as regular characters when used within square brackets.


2 Answers

This is actually two escapes in different contexts, due to the fact that the argument is a regular expression represented as a string.

The [ has to be escaped because otherwise it would have a special meaning in the regular expression. The escape for the regular expression would make it \[. But then the \ has to be escaped as it has special meaning in a string (for escaping and for representing characters by numeric value).

It can be much worse, as the \ character is used for escaping in both contexts. If you want to split by the \ character, you have to escape it (\\) for the regular expression usage, but then you have two \ characters, which both have to be escaped in string context. The usage in the original string you are splitting would also need the escape if you're writing it as a constant, so the analogous split would look like:

"hello\\world".split("\\\\")
like image 126
Don Roby Avatar answered Nov 09 '22 22:11

Don Roby


Different versions of Groovy give different results.

For Groovy 1.1-BETA-1 and onwards,

assert "hello[world".split("\\[") == ["hello", "world"]

asserts OK, but Groovy 1.0 and before gives an assertion error.

assert "hello[world".split(/\[/) == ["hello", "world"]

asserts OK for all versions from 1.1-BETA-1 onwards, Groovy 1.0-jsr-01 to 1.0 gives an assertion error, while Groovy 1.0-beta-10 and before gives a lexical error.

"hello[world".split("\\[").each{println it}

for all versions of Groovy from 1.0-beta-5, and beta-3, prints

hello
world

but 1.0-beta-4 prints

[.]
[.]
hello
world
like image 40
Vorg van Geir Avatar answered Nov 09 '22 22:11

Vorg van Geir