Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So, for example I have

StringReplace[
  {"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi", "acb", "acd"}, 
  {"bd" -> "", "bg" -> ""}]

which gives

{"abc", "a", "abx", "abf", "abe", "a", "abh", "abi", "acb", "acd"}, 

but I want the new list to be

{"abc", "abx", "abf", "abe", "abh", "abi", "acb", "acd"}.

A solution is alright if it works for the fact that I am starting with a list of sub-lists with the same length and deleting everything else. I tried using an if statement with by splitting into characters and using delete, but for some reason this didn't work.

share|improve this question
    
does this give ehat you need: DeleteCases[{"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi", "acb", "acd"}, _?(StringMatchQ[#, "*bd" | "*bg"] &)]? – kglr 6 hours ago
    
Select[{"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi", "acb", "acd"}, ! StringContainsQ[#, {"bd", "bg"}] &] – V.E. 6 hours ago
up vote 1 down vote accepted
strngs = {"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi",  "acb", "acd"}; 

Pick[strngs, ! StringMatchQ[#, "*bd*" | "*bg*"] & /@   strngs]

{"abc", "abx", "abf", "abe", "abh", "abi", "acb", "acd"}

share|improve this answer
lst = {"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi", "acb", "acd"}
fn = Pick[#, StringFreeQ[#, #2]] &;


fn[lst, "bd" | "bg"]

{"abc", "abx", "abf", "abe", "abh", "abi", "acb", "acd"}

fn[lst, "bd" | "ac" | "bc"]

{"abx", "abf", "abe", "abg", "abh", "abi"}

Etc...

share|improve this answer
    
+1 Similar: Select[lst, StringFreeQ["bd" | "bg"]] or Cases[lst, _?(StringFreeQ["bd" | "bg"])] – WReach 5 hours ago

Replacing works too:

Replace[_?(StringContainsQ["bd" | "bg"]) -> Sequence[]] /@
{"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi", "acb", "acd"}
share|improve this answer

You could use StringDelete

data = {"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi", "acb", "acd"};
StringDelete[data, {_ ~~ "bd", _ ~~ "bg"}] // DeleteCases[""]

{"abc", "abx", "abf", "abe", "abh", "abi", "acb", "acd"}

An alternative is

StringDelete[data, (_ ~~ "bd") | (_ ~~ "bg")] // DeleteCases[""]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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