Re: a conflicting StringReplace

*To*: mathgroup at smc.vnet.net*Subject*: [mg56367] Re: [mg56306] a conflicting StringReplace*From*: Michael_Harre at resimac.com.au*Date*: Fri, 22 Apr 2005 06:25:50 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

Hi Hui, Mathematica looks at the first element of the initial string and decides whether or not it is the beginning of a string that matches the replacement rule, so In[1] StringReplace["abc",{"ab"->"AB"}] Out[1] ABc found "ab" in the initial string matches "ab" in the replacement rule and so replaces "ab" with "AB", then Mathematica looks at the next letter after the string that has just been substituted in. So in your students example, after the first string is replaced the next element to be looked at is "c", not "b". An example might help here: In[2] StringReplace["abc", {"ab" -> "Q", "bc" -> "ED"}] Out[2] Qc because "bc" does not match anything in the remainder of the string (which is just "c"), However: In[3] StringReplace["abc", {"ab" -> "Q", "c" -> "ED"}] Out[3] QED Mike. -----Original Message----- From: Hui Fang [mailto:fangh73 at xmu.edu.cn] To: mathgroup at smc.vnet.net Subject: [mg56367] [mg56306] a conflicting StringReplace I was teaching Mathematica in a college. In the class I was showing them some built-in functions about strings. Since this is not a very important issue, I didn't spend much time on each function. When I show them StringReplace, I gave them the following examples: In[1] StringReplace["abc",{"ab"->"AB"}] Out[1] ABc In[2] StringReplace["abc", {"bc"->"BC"}] Out[2] aBC No problem on those. Now a student tried the following: In[3] StringReplace["abc", {"ab"->"AB", "bc"->"BC"}] Out[3] ABc Now he asked me why only "ab" is replaced. I said this is because there is a conflict because both "ab" and "bc" contains "b". So Mathematica will choose the first replacement. I also told him if he changes the order, he will get aBC. Now: In[4] StringReplace["abc", {"bc"->"BC","ab"->"AB"}] Out[4] ABc This is the part I don't understand. Does Mathematica treat those rules in their canonical order (since "ab" is before "bc" in canonical order.), or in their written order? Thanks a lot! Hui Fang ***Confidential Communication*** This message is directed to the addressee. If you are not the intended recipient, any use, disclosure or copying of the information contained herein is unauthorised. If you have received this message in error, please phone 02 9248 0300. Confidentiality and legal privilege are not waived or lost through mistaken delivery to you. Views expressed in this message are those of the individual sender, and may not necessarily represent the views of the Resimac Limited. There is no warranty that this message is error or virus free. ***Unsubscribe Facility*** You may unsubscribe to this message by replying to the sender's email address with the subject heading "UNSUBSCRIBE". Please note that by doing so, you may prevent further electronic communication between us that will undermine any commercial relationship which may exist.