2
$\begingroup$

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.

| improve this question | | | | |
$\endgroup$
  • $\begingroup$ does this give ehat you need: DeleteCases[{"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi", "acb", "acd"}, _?(StringMatchQ[#, "*bd" | "*bg"] &)]? $\endgroup$ – kglr Jan 29 '17 at 0:55
  • $\begingroup$ Select[{"abc", "abd", "abx", "abf", "abe", "abg", "abh", "abi", "acb", "acd"}, ! StringContainsQ[#, {"bd", "bg"}] &] $\endgroup$ – V.E. Jan 29 '17 at 0:58
2
$\begingroup$
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"}

| improve this answer | | | | |
$\endgroup$
4
$\begingroup$
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...

| improve this answer | | | | |
$\endgroup$
  • 1
    $\begingroup$ +1 Similar: Select[lst, StringFreeQ["bd" | "bg"]] or Cases[lst, _?(StringFreeQ["bd" | "bg"])] $\endgroup$ – WReach Jan 29 '17 at 1:41
1
$\begingroup$

Replacing works too:

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

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[""]
| improve this answer | | | | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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