String DOUBLE_BACKSLASH= "\\" ought to not show an error.

Answered

I use this construct in reg.exp.. It's perfectly legal Java. Intellij shows it as an error. It's useful to escape other patterns, like opening parens '(' which would otherwise signal to the pattern compiler you're starting a group.

It took me a long time to understand it wasn't an error coming from Java but from IJ, which appears to actually be looking ahead in my code, determining the construct is likely to be used in reg. exp. and deciding that I am about to make an error. 

I concluded that by  commenting out the context (Pattern compilation) in which I was using it and watching the error disappear as soon as I did so.

It took me a while to understand what  was going on. 

It didn't help that the toString() on a Pattern will show the processed reg.exp. -complete with pattern escape backslashes. I was unaware of that and I think most people might be surprised even though it's probably the right thing to do.  When I Pattern.toString() to examine it, It looked like I had an extra backslash when in fact I had the right number in the eyes of Pattern.compile(), namely one.

 

This combined with the erroneous IJ error marking was confusing. 

0
2 comments

If the inspection worked incorrectly, please report an issue at https://youtrack.jetbrains.com/newIssue?project=IDEA. A complete code fragment to reproduce the issue would be appreciated.

Note that you can always suppress inspections for lines/files, etc: https://www.jetbrains.com/help/idea/disabling-and-enabling-inspections.html#suppress-inspections .

0

Please sign in to leave a comment.