Using DeleteCases to match a string pattern

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












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.










share|improve this question









New contributor




Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    3
    down vote

    favorite
    1












    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.










    share|improve this question









    New contributor




    Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      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.










      share|improve this question









      New contributor




      Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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.







      pattern-matching filtering






      share|improve this question









      New contributor




      Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 21 mins ago









      Carl Woll

      63.3k282163




      63.3k282163






      New contributor




      Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 36 mins ago









      Karl

      163




      163




      New contributor




      Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Karl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          First note the FullForm of your pattern:



          __ ~~ "err" //FullForm



          StringExpression[BlankSequence,"err"]




          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]











          share|improve this answer



























            up vote
            2
            down vote













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






            For versions prior to version 10:



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









            share|improve this answer






















            • 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?
              – Karl
              16 mins ago










            • @Karl, please see Carl' Woll's answer for a detailed explanation.
              – kglr
              12 mins ago






            • 1




              @Karl, _ pattern matches any Mathematica expression; only in string patterns it stands for a single character.
              – kglr
              6 mins ago











            Your Answer





            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "387"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "",
            contentPolicyHtml: "",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Karl is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184973%2fusing-deletecases-to-match-a-string-pattern%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            First note the FullForm of your pattern:



            __ ~~ "err" //FullForm



            StringExpression[BlankSequence,"err"]




            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]











            share|improve this answer
























              up vote
              4
              down vote













              First note the FullForm of your pattern:



              __ ~~ "err" //FullForm



              StringExpression[BlankSequence,"err"]




              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]











              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









                First note the FullForm of your pattern:



                __ ~~ "err" //FullForm



                StringExpression[BlankSequence,"err"]




                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]











                share|improve this answer












                First note the FullForm of your pattern:



                __ ~~ "err" //FullForm



                StringExpression[BlankSequence,"err"]




                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]












                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 17 mins ago









                Carl Woll

                63.3k282163




                63.3k282163




















                    up vote
                    2
                    down vote













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






                    For versions prior to version 10:



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









                    share|improve this answer






















                    • 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?
                      – Karl
                      16 mins ago










                    • @Karl, please see Carl' Woll's answer for a detailed explanation.
                      – kglr
                      12 mins ago






                    • 1




                      @Karl, _ pattern matches any Mathematica expression; only in string patterns it stands for a single character.
                      – kglr
                      6 mins ago















                    up vote
                    2
                    down vote













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






                    For versions prior to version 10:



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









                    share|improve this answer






















                    • 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?
                      – Karl
                      16 mins ago










                    • @Karl, please see Carl' Woll's answer for a detailed explanation.
                      – kglr
                      12 mins ago






                    • 1




                      @Karl, _ pattern matches any Mathematica expression; only in string patterns it stands for a single character.
                      – kglr
                      6 mins ago













                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









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






                    For versions prior to version 10:



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









                    share|improve this answer














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






                    For versions prior to version 10:



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










                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 19 mins ago

























                    answered 32 mins ago









                    kglr

                    168k8192395




                    168k8192395











                    • 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?
                      – Karl
                      16 mins ago










                    • @Karl, please see Carl' Woll's answer for a detailed explanation.
                      – kglr
                      12 mins ago






                    • 1




                      @Karl, _ pattern matches any Mathematica expression; only in string patterns it stands for a single character.
                      – kglr
                      6 mins ago

















                    • 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?
                      – Karl
                      16 mins ago










                    • @Karl, please see Carl' Woll's answer for a detailed explanation.
                      – kglr
                      12 mins ago






                    • 1




                      @Karl, _ pattern matches any Mathematica expression; only in string patterns it stands for a single character.
                      – kglr
                      6 mins ago
















                    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?
                    – Karl
                    16 mins ago




                    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?
                    – Karl
                    16 mins ago












                    @Karl, please see Carl' Woll's answer for a detailed explanation.
                    – kglr
                    12 mins ago




                    @Karl, please see Carl' Woll's answer for a detailed explanation.
                    – kglr
                    12 mins ago




                    1




                    1




                    @Karl, _ pattern matches any Mathematica expression; only in string patterns it stands for a single character.
                    – kglr
                    6 mins ago





                    @Karl, _ pattern matches any Mathematica expression; only in string patterns it stands for a single character.
                    – kglr
                    6 mins ago











                    Karl is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Karl is a new contributor. Be nice, and check out our Code of Conduct.












                    Karl is a new contributor. Be nice, and check out our Code of Conduct.











                    Karl is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184973%2fusing-deletecases-to-match-a-string-pattern%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery