History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GROOVY-2451
Type: Bug Bug
Status: Closed Closed
Resolution: Won't Fix
Priority: Major Major
Assignee: Paul King
Reporter: Antti Karanta
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
groovy

regex with / and \ causes compilation error on 1.5.1

Created: 27/Dec/07 04:15 AM   Updated: 02/Jan/08 12:27 AM
Component/s: None
Affects Version/s: 1.5.1
Fix Version/s: 1.5.2

Time Tracking:
Not Specified

Environment: win xp sp2, also see below


 Description  « Hide
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:\>



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Antti Karanta - 27/Dec/07 04:19 AM
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:\>

Paul King - 28/Dec/07 05:46 AM
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/

Antti Karanta - 28/Dec/07 06:06 AM
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?


Paul King - 28/Dec/07 02:59 PM
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.

Paul King - 28/Dec/07 03:00 PM
Doco was improved slightly to highlight this gotcha - further clarifications/additions welcome ...

Antti Karanta - 31/Dec/07 01:50 AM
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).



Antti Karanta - 02/Jan/08 12:27 AM
I think the addition you made to the docs makes this very clear. I have nothing to add to that.