MATLAB: How to build a pattern expression that does NOT match another pattern

MATLABpatternpattern matching

As an exercise to learn pattern matching, I was trying to make a pattern that matches valid variable names. Here are the rules I'm trying to encode in a pattern:
  1. A valid variable name begins with a letter and contains not more than namelengthmax characters.
  2. Valid variable names can include letters, digits, and underscores.
  3. MATLAB keywords are not valid variable names.
I'm stuck trying to figure out how to make the pattern not match MATLAB keywords. Is this possible as of R2020b? Any workarounds? Here's what I was trying to go for…
function pat = varnamePattern
varchars = lettersPattern(1) + asManyOfPattern( alphanumericsPattern(1) | "_" , 0 , namelengthmax - 1 ); %rules 1 & 2
keywords = pattern(iskeyword); %pattern that matches MATLAB keywords
varname = varchars & ~keywords; %<-- invalid syntax. '&' and '~' not supported.
pat = namedPattern(varname,'varname','A valid MATLAB variable name')
end
As far as I can tell, wildcardPattern – with the optional parameter "Except" – is the only object function that considers "not" rules like #3. Unfortunately, the parameter value cannot be a string or cell array, and wildcardPattern seems meant for individual characters since it's lazy. I may be able to write an expression with regexpPattern, but I have no idea what that regular expression would look like.

Best Answer

A word is not a valid MATLAB variable name if:
  • it is more than namelengthmax characters no matter what those characters are
  • it is one of the keywords; or
  • any character is something that is not letters, digits, underscores; or
  • it starts with a non-letter
Each of those should be easy to construct. For example "more than namelengthmax" is the "any character" pattern namelengthmax+1 or more times. Wildcard "Except" letters, digits, underscore is the "any character that is not" rule.