Does replacing a list of strings have to be this convoluted (with a warning too)? Why can't ReplaceAll natively use string matching patterns?
s = {"dir abc", "wfori", "dir ej99", "lkdwk", "pokp"};
s /. {x_ /; StringMatchQ[x, "dir " ~~ ___] -> "X"}
s /. {"dir " ~~ ___ -> "X"}
(* {"X", "wfori", "X", "lkdwk", "pokp"} )
( {"dir abc", "wfori", "dir ej99", "lkdwk", "pokp"} *)
I know there's StringReplace but then, it hasn't the "built-in" ability to remove an element like ReplaceAll does
s /. {x_ /; StringMatchQ[x, "dir " ~~ ___] -> Nothing}
StringReplace[{"dir abc", "wfori", "dir ej99", "lkdwk", "pokp"}, "dir " ~~ ___ -> Nothing]
(* {"wfori", "lkdwk", "pokp"} )
( {StringExpression[Nothing], "wfori", StringExpression[Nothing], "lkdwk", "pokp"} *)
Select[StringFreeQ["dir"]][s]– Syed Dec 07 '22 at 11:25ReplaceAllcan't work at all... For anything other but strings I could useReplaceAllboth for actual replacing or "delete" something from a list and this capability is much powerful than relying on two different methods to replace/delete strings in a list... Does it have to be this way? – Domenico Modica Dec 07 '22 at 11:46StringReplaceonly works on strings. If you make the pattern more restrictive by replacingx_withx_Stringthen you get no warning. – Gustavo Delfino Dec 07 '22 at 12:49