I've got this line of code:

Pattern pattern = Pattern.compile('(?:([\\w&&[^_]]+)|.)+');

However, I'm getting this error in the Developer Console:

Variable pattern is used before it is declared

What am I doing wrong?


Apex is case insensitive.

Apex is trying to use the variable you are declaring instead of referencing the class. To Apex, pattern and Pattern are the same thing, and at the moment pattern is closest in scope. Try renaming it to p or patternA or whatever makes most sense for your code.

  • 10
    Or, if you must use the variable name pattern (why, oh, why?!) you can always refer to System.Pattern to avoid confusion: System.Pattern pattern = System.Pattern.compile(...); I'd avoid doing this, personally. Things tend to break.
    – sfdcfox
    Apr 10 '15 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.