Skip to main content
3 of 10
added 51 characters in body
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375

I don't think that StringMatchQ["x", Except[{"*"}]] works as expected as well as StringMatchQ["x", Except["*"]]:

StringMatchQ["x", Except[{"*"}]]
StringMatchQ["x", Except["*"]]
True
True

The string pattern "*" is an abbreviated string pattern consisted from the only metacharacter * which corresponds to zero or more characters according to the first point under the "Details and Options" section of StringMatchQ. So by the definition the pattern Except["*"] shouldn't give a match at all because it means negation of literally everything. Also for matching verbatim "*" one must escape this metacharacter with double backslashes "\\*" (the reference is the same). So current behavior is a bug.

The following is a set of examples of the expected behavior:

StringMatchQ["", "*"]
True
StringMatchQ["x", {"*"}]
StringMatchQ["xy", "*"]
True
True
StringMatchQ["*", Except[{"*"}]]
StringMatchQ["*", Except["*"]]
StringMatchQ["xy", Except["*"]]
False
False
False
StringMatchQ["x", "@"]
True
StringMatchQ["x", {"\\*"}]
StringMatchQ["x", "\\*"]    
StringMatchQ["*", "\\*"]
False

False

True

The following is a set of examples of wrong behavior of Except with metacharacters:

StringMatchQ["x", Except[{"\\*"}]] (* metacharacter is correctly escaped *)
StringMatchQ["x", Except["\\*"]] (* metacharacter is correctly escaped *)

StringExpression::invld: Element Except[{\}] is not a valid string or pattern element in Except[{\}]. >>

StringMatchQ["x", Except[{"\\*"}]]

StringExpression::invld: Element Except[\] is not a valid string or pattern element in Except[\]. >>

StringMatchQ["x", Except["\\*"]]
StringMatchQ["x", Except["\*"]] (* metacharacter is wrongly escaped *)
StringMatchQ["*", Except["\*"]] (* metacharacter is wrongly escaped *)
True
True
StringMatchQ["x", Except["@"]]

StringExpression::invld: Element Except[@] is not a valid string or pattern element in Except[@]. >>

StringMatchQ["x", Except["@"]]
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375