I would like to replace lists within a list based on a string pattern with a blank sequence:
list = {{"", "abc"}, {"", "ooobc"}};
rules = {{_, "abc"} -> "x", {_, ___~~ "bc"}->"y"};
ReplaceAll[list, rules]
{"x", {"", _ ~~ "ooobc"}}
I would like to replace {"", "ooobc"} with "y". Since I tried from different angles - is there an elegant approach to use ReplaceAll with some kind of StringContainsQ logic?
Edit:
One of the comments solves my problem well, so for completeness I add here:
rules = {{_, "abc"} -> "x", {_, _?(StringMatchQ[___ ~~ "bc"])} -> "y"};
rules = {{_String, _?(StringMatchQ["abc", #] &)} -> "x"};work for you? – Lukas Lang Sep 26 '17 at 19:02rules = {{_, "abc"} -> "x", {_, x_} /; StringContainsQ[x, "bc"] -> "y"};like that? – N.J.Evans Sep 26 '17 at 19:17StringContainsQ:rules = {{_, "abc"} -> "x", {_, _?(StringMatchQ[___ ~~ "bc"])} -> "y"};– Lukas Lang Sep 26 '17 at 19:19