Message Boards Message Boards

[✓] Simple rewrite rule using String function works but shows error?


I used Matlab/Mathematica during my PhD in 1993 and now trying to learn the fascinating Wolfram Language. So this might be a newby question so bear with me. The below simple expression

{"a.del", "b.del", "c.del"} /. s__ -> StringDelete[s, ".del"]

generates the expectecd content

{"a", "b", "c"}

but generates also the following error

StringDelete::strse: String or list of strings expected at position 1 in StringDelete[s,.del].

What am I doing wrong? Thanks for your help. Regards Markus

POSTED BY: Markus Sonderegger
1 month ago

Your syntax is a little unusual, but it did work. The error message is coming from the rule, which gets evaluated before being applied. It is at the lowest level, the second part of

ReplaceAll[string, rule]

where the rule has its right hand side StringDelete[s,".del"] and s is just a symbol so it generates that message. But it stays unevaluated, and once it has a valid list of strings instead of s it works.

The way I would expect this to be entered would be something like

StringDelete[{"a.del", "b.del", "c.del"}, ".del"]
POSTED BY: Todd Rowland
1 month ago

No error message is generated if RuleDelayed is used instead of Rule:

In[1]:= {"a.del", "b.del", "c.del"} /. s__ :> StringDelete[s, ".del"]

Out[1]= {"a", "b", "c"}
POSTED BY: Hans Milton
1 month ago

Notice that these also work:

In[1]:= StringDelete[".del"] /@ {"a.del","b.del","c.del"}
Out[1]= {a,b,c}

In[2]:= StringDelete[".del"][{"a.del","b.del","c.del"}]
Out[2]= {a,b,c}

In[3]:= StringDelete[{"a.del","b.del","c.del"},".del"]
Out[3]= {a,b,c}
POSTED BY: Gustavo Delfino
1 month ago

Group Abstract Group Abstract