Re: a conflicting StringReplace
- To: mathgroup at smc.vnet.net
- Subject: [mg56363] Re: [mg56306] a conflicting StringReplace
- From: Hui Fang <fangh73 at xmu.edu.cn>
- Date: Fri, 22 Apr 2005 06:25:12 -0400 (EDT)
- Organization: Xiamen University Photonics Center
- References: <4FDA877403669E48A2CB7F1122A7948045C785@dassne02.da.gei>
- Reply-to: fangh73 at xmu.edu.cn
- Sender: owner-wri-mathgroup at wolfram.com
Thanks, Wolf. Your experiment is very convincing. Hui Wolf, Hartmut wrote: >The behavior is explained in Help: > >StringReplace goes through a string, testing substrings that start at >each successive character position. On each substring, it tries in turn >each of the transformation rules you have specified. If any of the rules >apply, it replaces the substring, then continues to go through the >string, starting at the character position after the end of the >substring. > > >What is not quite clear (to me) from that explanation is the meaning of >"each substring": does this just mean the rest of the string starting at >current position, or is each substring of different length (starting >there) considered as different (such that the next rule is tried first, >before we further run down the string? An experiment shows, that the >first assumtion applies (and this gives the algorithm that performs >better) > > >StringReplace goes through a string, testing substrings that start at >each successive character position. > >-- starting at position of "a" in string "abc" > > >On each substring, > >-- at starting position it's: "abc" > >it [StringReplace] tries in turn each of the transformation rules you >have specified. > >-- so first it tries "b" of "bc" on "a" of "abc" --> fail, try next rule >-- then tries "a" of "ab" on "a" of "abc" --> interesting, go on >-- tries "b" of "ab" on "b" of "abc" --> interesting, go on >-- pattern is exhausted, such we have --> success of pattern on >substring "ab" of "abc" > >If any of the rules apply, it replaces the substring, > >-- so here "ab" of "abc" becomes "AB", i.e the string becomes > >"ABc" > >then continues to go through the string, starting at the character >position after the end of the substring. > >-- Substring considered next is "c" of "abc" (or "ABc", replaced part is >not considered again) > >-- so now we compare "b" of "bc" on "c" --> fail, try next rule >-- try "a" of "ab" on "c" --> fail, no more rule, advance position > >-- but string is exhausted > >-- so the result is "ABc" > > > > >Here are two more examples: > > >In[29]:= StringReplace["abc", {"ab" -> "ab", "bc" -> "bc", > "a" -> "1", "b" -> "2", "c" -> "3"}] >Out[29]= "ab3" > >here rule "a" -> "1" is masked by pattern "ab" which matches first, >"bc" and "b" cannot match, as neither substring "bc" nor "b" are part of >substring "c" left over from "abc" after substitution "ab" -> "ab". > > > >In[31]:= StringReplace["abc", {"a" -> "1", "ab" -> "ab", > "bc" -> "bc", "b" -> "2", "c" -> "3"}] >Out[31]= "1bc" > >here rule "a" -> "1" matches first, and substitution takes place. >For rest of string "bc", from "abc", patterns "a" and "ab" will never >match, "bc" -> "bc" matches first, >and such masks rule "c" -> "3" > > > > >-- >Hartmut Wolf > > > >