- 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
How to build a pattern expression that does NOT match another pattern?
18 views (last 30 days)
Show older comments
Zachary
on 8 Mar 2021
Commented: Walter Roberson
on 11 Mar 2021
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 = 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.
0 Comments
Accepted Answer
Walter Roberson
on 8 Mar 2021
A word is not a valid MATLAB variable name if:
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.
2 Comments
Walter Roberson
on 11 Mar 2021
Sure.
Any of these patterns:
any of 'adhjklmnquvxyzADHJKLMNQUVXYZ' followed by up to maxnamelengthmax-1 copies of (alphanumericsPattern | "_") followed by end of pattern
'b' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqstuvwxyz' followed by up to maxnamelengthmax-2 copies of (alphanumericsPattern | "_") followed by end of pattern
'br' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdfghijklmnopqrstuvwxyz' followed by up to maxnamelengthmax-3 copies of (alphanumericsPattern | "_") followed by end of pattern
'bre' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZbcdefghijklmnopqrstuvwxyz' followed by up to maxnamelengthmax-4 copies of (alphanumericsPattern | "_") followed by end of pattern
'brea' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijlmnopqrstuvwxyz' followed by up to maxnamelengthmax-5 copies of (alphanumericsPattern | "_") followed by end of pattern
'break' followed by at least 1 and at most maxnamelengthmax-5 copies of (alphanumericsPattern | "_") followed by end of pattern
'c' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZbcdefghijkmnopqrstuvwxyz' followed by up to maxnamelengthmax-2 copies of (alphanumericsPattern | "_") followed by end of pattern
'ca' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqruvwxyz' followed by up to maxnamelengthmax-3 copies of (alphanumericsPattern | "_") followed by end of pattern
'cas' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdfghijklmnopqrstuvwxyz' followed by up to maxnamelengthmax-4 copies of (alphanumericsPattern | "_") followed by end of pattern
'case' followed by at least 1 and at most maxnamelengthmax-4 copies of (alphanumericsPattern | "_") followed by end of pattern
'cat' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz' followed by up to maxnamelengthmax-4 copies of (alphanumericsPattern | "_") followed by end of pattern
'catc' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgijklmnopqrstuvwxyz' followed by up to maxnamelengthmax-5 copies of (alphanumericsPattern | "_") followed by end of pattern
'catch' followed by at least 1 and at most maxnamelengthmax-5 copies of (alphanumericsPattern | "_") followed by end of pattern
'cl' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZbcdefghijklmnopqrstuvwxyz' followed by up to maxnamelengthmax-3 copies of (alphanumericsPattern | "_") followed by end of pattern
'cla' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtuvwxyz' followed by up to maxnamelengthmax-4 copies of (alphanumericsPattern | "_") followed by end of pattern
'class' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijlmnopqrtuvwxyz' followed by up to maxnamelengthmax-5 copies of (alphanumericsPattern | "_") followed by end of pattern
'classd' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijlmnopqrstuvwxyz' followed by up to maxnamelengthmax-6 copies of (alphanumericsPattern | "_") followed by end of pattern
'classde' followed by any of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeghijlmnopqrstuvwxyz' followed by up to maxnamelengthmax-7 copies of (alphanumericsPattern | "_") followed by end of pattern
'classdef' followed by at least 1 and at most maxnamelengthmax-7 copies of (alphanumericsPattern | "_") followed by end of pattern
and so on.
Not a "not match" in sight.
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!