Search code examples
javaregexintellij-ideastructural-searchinspections

IntelliJ structural search regex questions


I need to match variables that start with a lowercase letter and don't end in an underscore.

I have these three fields:

private String shouldFlag;
private String shouldntFlag_;
private String SHOULDNTFLAG;

With this pattern inverted: ^[a-z].*_$

Used with for fieldname in the following template:

class $Class$ { 
  $FieldType$ $FieldName$ = $Init$;
}

The problem is that SHOULDNTFLAG is still flagged. I tried using ^[a-z].*_$|^[A-Z].*$, but that did not match anything, let alone just shouldFlag. What am I doing wrong here?


Solution

  • Assuming your variable names can only contain ASCII letters and digits plus the underscore, I would go with

    \b[a-z]\w*\b(?<!_)
    

    EDIT: ...and, as @Stefan pointed out, you need to select the "case-sensitive" option.