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:
- A valid variable name begins with a letter and contains not more than namelengthmax characters.
- Valid variable names can include letters, digits, and underscores.
- 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 = varnamePatternvarchars = 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