I am trying to remove all of the elements from a list that contain the characters "err" in that order.

I expect the following code to return an empty list:

DeleteCases[{"Aerr"}, __ ~~ "err"]

However it fails to recognize the pattern and will return the full List. What am I missing in the documentation for patterns? Perhaps it is an issue with DeleteCases, as I can use StringMatchQ to identify that the patterns match.

StringMatchQ["Aerr", __ ~~ "err"]

Returns True.


2 Answers 2


First note the FullForm of your pattern:

__ ~~ "err" //FullForm


The pattern is a StringExpression, so you must use a string function, e.g., StringMatchQ instead of DeleteCases, which is expecting a normal pattern. Here are some other possibilities:

list = {"Aerr"};

Select[list, Not @* StringMatchQ[__ ~~ "err"]]
Pick[list, StringMatchQ[list, __ ~~ "err"], False]



DeleteCases[{"Aerr"}, _?(StringMatchQ[ "*err"]) ]


For versions prior to version 10:

DeleteCases[{"Aerr"}, _?(StringMatchQ[#, "*err"] &)]


  • $\begingroup$ While I appreciate the working example, I would have loved further explanation of how it works. I've gathered that the "?" syntax is a pattern test, but how does the "_" pattern which is for a single character match "Aerr"? Also, is it a bug that the code in my original question didn't work, or a misunderstanding of how to use patterns? $\endgroup$
    – Karl
    Oct 31, 2018 at 0:16
  • $\begingroup$ @Karl, please see Carl' Woll's answer for a detailed explanation. $\endgroup$
    – kglr
    Oct 31, 2018 at 0:21
  • 1
    $\begingroup$ @Karl, _ pattern matches any Mathematica expression; only in string patterns it stands for a single character. $\endgroup$
    – kglr
    Oct 31, 2018 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.