Quantcast
Viewing all articles
Browse latest Browse all 186

Pattern matching with DeleteCases

I'm using Mathematica 14.0 and I'm having a pattern match problem with DeleteCases. The example below illustrates the problem. I define a test string followed by two patterns that both nominally match the test string. Next the test string and both patterns are used with DeleteCases and StringReplace. For DeleteCases pattern1 matches and the string is deleted but for pattern2 the pattern does not match and the string is not deleted. For StringReplace both patterns match and the string is replaced with theta.

Why does pattern2 not match for DeleteCases?

Input:

        testString = "2 -> ";        pattern1 = "2" ~~ " " ~~ "->" ~~ " ";        pattern2 = _ ~~ " " ~~ "->" ~~ " ";        DeleteCases[{testString}, pattern1 ]        DeleteCases[{testString}, pattern2 ]        StringReplace[{testString}, pattern1 -> "\[Theta]" ]        StringReplace[{testString}, pattern2 -> "\[Theta]" ]

Output:

        {}        {"2 -> "}        {"\[Theta]"}        {"\[Theta]"}        

Viewing all articles
Browse latest Browse all 186

Trending Articles