groovy

regex with / and \ causes compilation error on 1.5.1

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: 1.5.1
  • Fix Version/s: 1.5.2
  • Component/s: None
  • Labels:
    None
  • Environment:
    win xp sp2, also see below
  • Number of attachments :
    0

Description

C:\>groovy -v
Groovy Version: 1.5.1 JVM: 1.5.0_13-b05

C:\>groovy -e "'foo/bar' =~ /\/|
/"
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, script_from_command_line: 1: unexpected
char: '\' @ line 1, column 19.
1 error

C:\>programs\Java\Groovy1.0\bin\groovy -v
Groovy Version: 1.0 JVM: 1.5.0_13-b05

C:\>programs\Java\Groovy1.0\bin\groovy -e "'foo/bar' =~ /\/|
/"

C:\>

Activity

Hide
Antti Karanta added a comment -

Damn, the regex did not seem to come through. I'll try again:

C:\>groovy -v
Groovy Version: 1.5.1 JVM: 1.5.0_13-b05

C:\>groovy -e "'foo/bar' =~ /\/|\\/"
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, script_from_command_line: 1: unexpected
char: '\' @ line 1, column 19.
1 error


C:\>programs\Java\Groovy1.0\bin\groovy -v
Groovy Version: 1.0 JVM: 1.5.0_13-b05

C:\>programs\Java\Groovy1.0\bin\groovy -e "'foo/bar' =~ /\/|\\/"

C:\>
Show
Antti Karanta added a comment - Damn, the regex did not seem to come through. I'll try again:
C:\>groovy -v
Groovy Version: 1.5.1 JVM: 1.5.0_13-b05

C:\>groovy -e "'foo/bar' =~ /\/|\\/"
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, script_from_command_line: 1: unexpected
char: '\' @ line 1, column 19.
1 error


C:\>programs\Java\Groovy1.0\bin\groovy -v
Groovy Version: 1.0 JVM: 1.5.0_13-b05

C:\>programs\Java\Groovy1.0\bin\groovy -e "'foo/bar' =~ /\/|\\/"

C:\>
Hide
Paul King added a comment -

This is expected behavior. Inside a slashy string, backslashes don't escape anything except a forward slash. So your string isn't terminated, hence the compilation exception. I would probably write it something like this:

def bs = '\\\\'   // double escaped
def fs = '/'
assert 'foo/bar' =~ /$fs|$bs/
Show
Paul King added a comment - This is expected behavior. Inside a slashy string, backslashes don't escape anything except a forward slash. So your string isn't terminated, hence the compilation exception. I would probably write it something like this:
def bs = '\\\\'   // double escaped
def fs = '/'
assert 'foo/bar' =~ /$fs|$bs/
Hide
Antti Karanta added a comment -

You are right, now I have it working. Thanks.

I find it a little strange that this worked fine on older groovy versions. I suppose there was some good reason for breaking it?

If I got it right, there is now no way of matching a literal backslash in a regex short of creating the escaped backslash string outside the regex and then using gstring functionality to insert it into the regex (like it's done above)? A little annoying, IMO. Is this documented on some gotchas list?

Show
Antti Karanta added a comment - You are right, now I have it working. Thanks. I find it a little strange that this worked fine on older groovy versions. I suppose there was some good reason for breaking it? If I got it right, there is now no way of matching a literal backslash in a regex short of creating the escaped backslash string outside the regex and then using gstring functionality to insert it into the regex (like it's done above)? A little annoying, IMO. Is this documented on some gotchas list?
Hide
Paul King added a comment -

Yes, a little annoying but the old behavior had a much bigger set of gotcha rules. You could never place an odd number of consecutive backslashes into a slashy string. This was usually fine for regex scenarios but made windows path name and other scenarios a nightmare.

Show
Paul King added a comment - Yes, a little annoying but the old behavior had a much bigger set of gotcha rules. You could never place an odd number of consecutive backslashes into a slashy string. This was usually fine for regex scenarios but made windows path name and other scenarios a nightmare.
Hide
Paul King added a comment -

Doco was improved slightly to highlight this gotcha - further clarifications/additions welcome ...

Show
Paul King added a comment - Doco was improved slightly to highlight this gotcha - further clarifications/additions welcome ...
Hide
Antti Karanta added a comment -

I'd be happy to have a look at what you wrote about this in the docs, but I can't find it. Where did you put it?

And I overstated the effect this has - it's not possible to insert a backslash as the last character in a slashy string, in other positions it's fine (just saying this out loud in case someone else reads this bug report).

Show
Antti Karanta added a comment - I'd be happy to have a look at what you wrote about this in the docs, but I can't find it. Where did you put it? And I overstated the effect this has - it's not possible to insert a backslash as the last character in a slashy string, in other positions it's fine (just saying this out loud in case someone else reads this bug report).
Hide
Paul King added a comment -
Show
Paul King added a comment - Docs went here: http://groovy.codehaus.org/Strings#Strings-SlashyStringliterals
Hide
Antti Karanta added a comment -

I think the addition you made to the docs makes this very clear. I have nothing to add to that.

Show
Antti Karanta added a comment - I think the addition you made to the docs makes this very clear. I have nothing to add to that.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: